开发者

Questions about the GNU linker editor "ld"?

I have 2 obj files assembled with GNU as, they are:

  • a.o : my major program
  • b.o : some utility functions

a.o doesn't have an entry point. The final linked file will be loaded into memory and the exe开发者_如何学编程cution will jump to its very beginning loaded address, where is the first instrucion of a.o.

Now I want to link them together with GNU ld. And I want to make a.o appear before b.o in the final file. How could I control this? Do I have to make a custom section and write in the linker script like this:

SECTIONS
{
  . = 0x7c00;
  .text : { *(.text) }
  .my_custom_section : { *(.my_custom_section) }
  .data : { *(.data) }
  .bss  : { *(.bss) }
}

OUTPUT_FORMAT(binary)

Update

Is there something wrong with this question? Did I post it wrong? If so, please let me know, guys. Many thanks.

Currently, I found that the command line sequence of the input files seems to be relevant.

If I do like this:

ld a.o b.o -o final.bin

Content from a.o will appear before b.o.

If I do like this:

ld b.o a.o -o final.bin

It will be otherwise.

Is it meant to be controlled like this?


According to the manual:

options which refer to files ... cause the file to be read at the point at which the option appears in the command line, relative to the object files and other file options

So the order of files in the binary is the order in which they appear on the command line.

Therefore, it is meant to be controlled as you mention in your update.


The order of operations to ld is in fact relevant.

Unless explicitly stated somehow, the entry point is the first code byte of the first file on the list.

The resulting executable always has the contents of the .o files in invocation order. (with .a files it gets complicated).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜