* LD: Question about rewriting the function in library
@ 2022-04-15 8:18 夏立方
2022-04-19 10:14 ` Nick Clifton
0 siblings, 1 reply; 2+ messages in thread
From: 夏立方 @ 2022-04-15 8:18 UTC (permalink / raw)
To: binutils
Hi,
There is a simple case here:
a.s
-----------
.section .text.bar
.global bar
bar:
ret
.size bar, . - bar
.global foo
.text
.type foo, function
foo:
call bar
ret
.size foo, . - foo
----------
build: ./gas/as -o a.o a.s
AR: ./binutils/as -r liba.a a.o
And the 'main' in b.s will call 'foo', and I want to rewrite function 'bar'
b.s
---------
.global foo
.global bar
.text
bar:
nop
nop
ret
.size bar, . - bar
.global main
.text
main:
call foo
.size main, . - main
----------
build: ./gas/as -o b.o b.s
Link: ./ld/ld-new -o a b.o -L. -la
it will report 'multi define'.
Spliting 'bar' to an other object (c.o) could solve this problem. But the library in the project can't be re-implemented now.
-----------
From my understanding, overriding a function should not be related to other functions in the same section or in the same object.
So, my question is: Why not use the 'bar' in b.o?
Best Regards,
Lifang
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: LD: Question about rewriting the function in library
2022-04-15 8:18 LD: Question about rewriting the function in library 夏立方
@ 2022-04-19 10:14 ` Nick Clifton
0 siblings, 0 replies; 2+ messages in thread
From: Nick Clifton @ 2022-04-19 10:14 UTC (permalink / raw)
To: 夏立方, binutils
Hi 夏立方,
> a.s
> -----------
> .global bar
> bar:
> ret
> b.s
> ---------
> .global bar
> bar:
> nop
> nop
> ret
> it will report 'multi define'.
Correct.
> Spliting 'bar' to an other object (c.o) could solve this problem. But the library in the project can't be re-implemented now.
OK.
> From my understanding, overriding a function should not be related to other functions in the same section or in the same object.
> So, my question is: Why not use the 'bar' in b.o?
Because the bar symbol has been defined twice, once in a.o and once in b.o
and the linker does not allow this. (If the symbols are global symbols).
You have a couple of options to fix this problem. If you can edit the
sources of a.s, you could make the definition of bar weak:
a.s
------
.global bar
.weak bar
[...]
Weak symbols can be overridden by another symbol of the same name, so in
this case bar in a.o will only be used if another definition of bar cannot
be found. If you link a.o and b.o time, the definition of bar in b.o will
be used and the definition in a.o will be ignored.
If you cannot modify a.s however, then you can make use of a linker feature
that allows you to rename symbols. The "--wrap <symbolname>" option will
tell the linker that any reference to <symbolname> should be replaced with
a reference to __wrap_<symbolname>. So if you change the code in b.s to:
.global __wrap_bar
__wrap_bar:
nop
nop
ret
[...]
And then link with "--wrap bar" specified on the linker command line; the
call to "bar" in a.o will be transformed into a call "__wrap_bar" and so
it will be resolved by the definition in b.o.
I hope that this helps.
Cheers
Nick
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-04-19 10:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-15 8:18 LD: Question about rewriting the function in library 夏立方
2022-04-19 10:14 ` Nick Clifton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).