简体   繁体   中英

How to set the The section number of a symbol when compiling ELF binary?

The test is on 32-bit Linux, x86.

Suppose in my assembly program final.s , I have to load some library symbols, say, stdin@@GLIBC_2.0 , and I want to load these symbols in a fixed address.

So following instructions in this question , I did this:

echo ""stdin@@GLIBC_2.0" = 0x080a7390;" > symbolfile
echo ""stdin@GLIBC_2.0 (4)" = 0x080a7390;" >> symbolfile
gcc -Wl,--just-symbols=symbolfile  final.s -g

And when I checked the output of symbol table, I got this:

readelf -s a.out | grep stdin
53: 080a7390     4 OBJECT  GLOBAL DEFAULT  ABS stdin@@GLIBC_2.0
17166: 080a7390     0 NOTYPE  GLOBAL DEFAULT  ABS stdin@GLIBC_2.0 (4)

And comparing to a common ELF biary that requires stdin symbol:

readelf -s hello.out | grep stdin

17199: 0838b8c4     4 OBJECT  GLOBAL DEFAULT   25 stdin@@GLIBC_2.0
52: 0838b8c4     4 OBJECT  GLOBAL DEFAULT   25 stdin@GLIBC_2.0 (4)

So an obvious difference I found is that the Ndx column, say, the section number of my fixed position symbols are ABS . Please check the references here .

When executing the a.out , it throws a segmentation fault error.

So my question is, how to set the section number of the symbol fixed position?

I want to load these symbols in a fixed address.

You are importing these symbols from GLIBC. Unless you are doing a fully-static linking, you get no say in what address these symbols end up at.

So my question is, how to set the section number of the symbol

That question makes no sense: section number itself is meaningless and 25 may refer to .bss in one executable, but to .text in another.

Your section 25 just happens to be .bss on this particular system and for this particular build. Try building a fully-static binary, and you are likely to see section 24 instead.

Anyway, a normal executable gets stdin copied from libc.so.6 . You will do well to read this description of the process, and pay special attention to "Extra credit #2: Referencing shared library data from the executable" section.

But it may be easier to understand the fully-static case first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM