Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binutils - kernel - "_binary" meaning?

I am reading xv6 lectures. I have a file named initcode.S that is to be linked in the kernel.

Now two symbols are created that way :

    extern char _binary_initcode_start[], _binary_initcode_size[];

inside a function.

The lecture says :

as part of the kernel build process, the linker embeds that binary that defines two special symbols, _binary_initcode_starcode_size, indicating the location and size of the binary.

I understand that binutils is getting the address and the size of this assembled code.

I wonder about the notation : is it default ? my searches didn't prove that clearly.

  • _binary -> it is originally an assembly code

  • _initcode -> the name of my file

  • _start -> the parameter i am interested in.

It would imply that any assembly code compiled would have those variables too.

I have no proof of that, though.


The question is :

is _binary_myAsmFileHere_myParameterhere the default variable structure binutils give to the assembly file to export their address, size and so on ?

Could someone tell me if my assumption is right and if it is better than that : the rule

Thanks

like image 323
Kroma Avatar asked Apr 15 '26 17:04

Kroma


1 Answers

Strangely enough, it doesn't seem to be documented in the ld manual. However, man objcopy does say this:

You can access this binary data inside a program by referencing the special symbols that are created by the conversion process. These symbols are called _binary_objfile_start, _binary_objfile_end and _binary_objfile_size. e.g. you can transform a picture file into an object file and then access it in your code using these symbols.

Apparently the same logic is used by ld when embedding binary files. Notice that the Makefile for xv6 contains this line for linking the kernel:

$(LD) $(LDFLAGS) -T kernel.ld -o kernel entry.o $(OBJS) -b binary initcode entryother

As you can see, it uses -b binary to embed the files initcode and entryother, so the above symbols will be defined during this process.

like image 177
Jester Avatar answered Apr 17 '26 07:04

Jester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!