symbolFor
is unpredictable in the presence of function symbol aliases
#25
Labels
bug
Something isn't working
Consider the following C program:
This defines a function
foo
, which gets compiled into a function symbol withGLOBAL
visibility when compiled to an ELF binary. It also defines a functionfoo_weak
, which uses a GCC-/Clang-specific attribute to compile it to a function symbol withWEAK
visibility. Moreover,foo_weak
is defined as an alias forfoo
, so both function symbols are placed at the same address in the ELF binary:(The technique of defining weak aliases is somewhat common in
musl
.)Having multiple function symbols at the same address like this confuses
macaw-loader
'ssymbolFor
function. To explain what I mean, let's use the followingmacaw-loader
–based driver program to load the function symbol at address0x1130
:Well, I say "the function symbol", but which one in particular? As it turns out,
symbolFor
is somewhat unpredictable in which symbol it picks. On the program above, it happens to pickfoo_weak
:Let's suppose we renamed
foo_weak
to something else, however:Now, the driver program picks
foo
instead ofabc
!The explanation for what we see above is that internally,
macaw-loader-x86
creates aMap
of symbol addresses to symbol names. The order in whichmacaw-loader-x86
happens to encounter each symbol in the binary determines which symbol name is inserted into theMap
. In the example above, the user-defined symbols in the binary happen to be sorted in more-or-less alphabetical order, so the symbol name that comes last alphabetically will be the one that is ultimately placed in theMap
.This is quite unfortunate, as it means that you're never quite sure which symbol you're going to get. Moreover, I have a downstream application that uses
symbolFor
to map function addresses to names, and if a binary defines weak symbols, then this can cause confusion whensymbolFor
picks a weak symbol name instead of a globally visible name (most users would expect the globally visible names).Possible ways of addressing this issue:
symbolFor
to return a non-empty list of all the symbol names associated with that address.symbolFor
the same, but also introduce another class method that can be used to query all of the symbol names associated with an address. Document thatsymbolFor
picks one symbol name arbitrarily.The text was updated successfully, but these errors were encountered: