1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // as.c -- map assembler file, scan for debug information. If found, expand file by one character and insert Cforall |
---|
8 | // language code on the N line from the start of the debug information. |
---|
9 | // |
---|
10 | // Author : Peter A. Buhr |
---|
11 | // Created On : Wed Aug 1 10:49:42 2018 |
---|
12 | // Last Modified By : Peter A. Buhr |
---|
13 | // Last Modified On : Tue Dec 7 21:15:25 2021 |
---|
14 | // Update Count : 132 |
---|
15 | // |
---|
16 | |
---|
17 | #include <cstdio> // perror |
---|
18 | #include <cstdlib> // exit |
---|
19 | #include <fcntl.h> // open |
---|
20 | #include <cstring> // strstr, memmove |
---|
21 | #include <unistd.h> // ftruncate,execvp |
---|
22 | #include <sys/stat.h> // fstat |
---|
23 | #include <sys/mman.h> // mmap |
---|
24 | |
---|
25 | //#define __DEBUG_H__ |
---|
26 | |
---|
27 | int main( const int argc, const char * argv[] ) { |
---|
28 | #ifdef __DEBUG_H__ |
---|
29 | for ( int i = 0; i < argc; i += 1 ) { |
---|
30 | fprintf( stderr, "%s\n", argv[i] ); |
---|
31 | } // for |
---|
32 | #endif // __DEBUG_H__ |
---|
33 | |
---|
34 | int fd = open( argv[argc - 1], O_RDWR ); |
---|
35 | if ( fd < 0 ) { perror( "open" ); exit( EXIT_FAILURE ); }; |
---|
36 | |
---|
37 | struct stat mystat = {}; |
---|
38 | if ( fstat( fd, &mystat ) ) { perror( "fstat" ); exit( EXIT_FAILURE ); }; |
---|
39 | off_t size = mystat.st_size; |
---|
40 | |
---|
41 | if ( size ) { // cannot map 0 sized file |
---|
42 | char * start = (char *)mmap( NULL, size + 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); |
---|
43 | if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); }; |
---|
44 | |
---|
45 | char * dcursor; |
---|
46 | if ( (dcursor = strstr( start, ".Ldebug_info0:" ) ) ) { // debug information ? |
---|
47 | |
---|
48 | if ( char * cursor = strstr( dcursor, ".long\t.LASF" ) ) { // language code ? |
---|
49 | for ( int i = 0; i < 2; i += 1 ) { // move N (magic) lines forward |
---|
50 | cursor = strstr( cursor, "\n" ) + 1; |
---|
51 | } // for |
---|
52 | cursor -= 1; // backup over '\n' |
---|
53 | // From elfcpp/dwarf.h in the binutils source tree. |
---|
54 | // DW_LANG_C89 = 0x1, DW_LANG_C = 0x2, DW_LANG_C99 = 0xc, DW_LANG_C11 = 0x1d |
---|
55 | if ( *(cursor - 3) == '0' && *(cursor - 2) == 'x' && |
---|
56 | (*(cursor - 1) == 'c' || *(cursor - 1) == '1' || *(cursor - 1) == '2') ) { // C89/C/C99 |
---|
57 | // Expand file by one byte to hold 2 character Cforall language code. |
---|
58 | if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); }; |
---|
59 | memmove( cursor + 1, cursor, start + size - cursor ); // move remaining text 1 character right |
---|
60 | } else if ( *(cursor - 4) == '0' && *(cursor - 3) == 'x' && *(cursor - 2) == '1' && *(cursor - 1) == 'd' ) { // C11 |
---|
61 | } else { |
---|
62 | for ( int i = 0; i < 6; i += 1 ) { // move N (magic) lines forward |
---|
63 | cursor = strstr( cursor, "\n" ) + 1; |
---|
64 | } // for |
---|
65 | fprintf( stderr, "*** ERROR *** Invalid C language code found in assembler file: %s\n" |
---|
66 | "Assembler debug information:\n%.*s", |
---|
67 | argv[argc - 1], (int)(cursor - dcursor), dcursor ); |
---|
68 | exit( EXIT_FAILURE ); |
---|
69 | } // if |
---|
70 | |
---|
71 | *(cursor - 2) = '2'; // replace C89/C/C99/C11 language code with CFA code |
---|
72 | *(cursor - 1) = '5'; |
---|
73 | } // if |
---|
74 | } // if |
---|
75 | |
---|
76 | if ( munmap( start, size + 2 ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk |
---|
77 | } // if |
---|
78 | |
---|
79 | argv[0] = "as"; |
---|
80 | execvp( argv[0], (char * const *)argv ); // should not return |
---|
81 | perror( "CFA Translator error: cpp level, execvp" ); |
---|
82 | exit( EXIT_FAILURE ); // tell gcc not to go any further |
---|
83 | } // main |
---|
84 | |
---|
85 | // Local Variables: // |
---|
86 | // tab-width: 4 // |
---|
87 | // mode: c++ // |
---|
88 | // compile-command: "g++ -Wall -Wextra as.c -o as" // |
---|
89 | // End: // |
---|