Inserting labels before and after my program's global variables

Amittai Aviram amittai.aviram@yale.edu
Fri Dec 17 16:24:00 GMT 2010


On Dec 17, 2010, at 10:23 AM, Ian Lance Taylor wrote:

> Amittai Aviram <amittai.aviram@yale.edu> writes:
> 
>> I would like to insert labels into my executable as it is being linked
>> so that there is a "start" label just before my user-defined global
>> variables in the .bss section and an "end" right after them, when I
>> use static linkage.  Thus the output of .bss from objdump -D would
>> include some lines something like this, supposing I had global
>> variables global_a and global_b:
> 
> This is the kind of thing which is much more easily done using a linker
> script.
> 
> 
>> Here is what I tried (but it didn't work).  I wrote a file head.s and
>> one called tail.s:
> 
>> gcc -static -o hello head.s hello.c tail.s
> 
> This does not actually put the files first and last.  To see why not,
> add the -v option and look at the linker command.  To put the files
> first and last, you will have to use -nostdlib to disable gcc's default
> start files and libraries, and then replicate them on the command line
> yourself, with head and tail where you want them.
> 
> Ian

Thank you very much!  But I just tried linking with the -v option, and the output looks as if the linker does in fact put head.o immediately before hello.o and tail.o immediately after it.  (Output below.  The relevant bit is near the bottom.)   Why doesn't that give me the results I was expecting in the finished executable binary?

$ make
gcc -c -o head.o head.s
gcc -c -o hello.o hello.c
gcc -c -o tail.o tail.s
gcc -Wall -static -g -v -o l1 head.o hello.o tail.o
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Wall' '-static' '-g' '-v' '-o' 'l1' '-mtune=generic'
 /usr/lib/gcc/x86_64-linux-gnu/4.4.3/collect2 --build-id -m elf_x86_64 --hash-style=both -static -o l1 -z relro /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtbeginT.o -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../.. head.o hello.o tail.o --start-group -lgcc -lgcc_eh -lc --end-group /usr/lib/gcc/x86_64-linux-gnu/4.4.3/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crtn.o


Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram@yale.edu
http://www.amittai.com




More information about the Gcc-help mailing list