Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include binary file with GNU ld linker script

Tags:

binary

gnu

ld

I have a working linker script. I want to add another data section whose contents is pulled directly from a file (ld shouldn't parse it and extract the sections and so on). How can I do that?

OUTPUT_FORMAT("elf32-i386") ENTRY(start) SECTIONS {   .text 0x100000 : {     *(.multiboot)     *(.text)     *(.code)     *(.rodata*)   }   .data : {     *(.data)   }   .bss : {     *(.bss)   }   kernel_end = .;   roottask_start = .;   .data : {      HERE I WANT TO INCLUDE THE ENTIRE CONTENTS OF ANOTHER (BINARY) FILE    }   roottask_end = .; } 
like image 825
Pandemonium Avatar asked Nov 29 '08 15:11

Pandemonium


People also ask

How do you specify a linker script?

You can use the ` --verbose ' command line option to display the default linker script. Certain command line options, such as ` -r ' or ` -N ', will affect the default linker script. You may supply your own linker script by using the ` -T ' command line option.

Does GCC include LD?

As you mentioned, gcc merely acts as a front-end to ld at link time; it passes all the linker directives (options, default/system libraries, etc..), and makes sure everything fits together nicely by taking care of all these toolchain-specific details for you.

What is Ld file in C?

An LD file is a script written in the GNU "linker command language." It contains one or more commands that are used to configure how input files storing static object code are to be compiled into a single executable program or library for the GNU operating system.


2 Answers

You could try using objcopy to convert it to a normal object you can link in, and then reference its symbols in the linker script like you would do to a normal object. From the objcopy manual page:

-B bfdarch --binary-architecture=bfdarch Useful when transforming a raw binary input file into an object file. In this case the output architecture can be set to bfdarch. This option will be ignored if the input file has a known bfdarch. 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.

...where objfile will be expanded to the name of the input object file.

See also the --rename-section option.

like image 157
CesarB Avatar answered Oct 06 '22 14:10

CesarB


You can put raw file to separate section in assembly, and then include this section in linker script.

First you need to create template .S file, eg.

.section .rawdata .incbin "blob1.raw" 

... and modify linker script to include this section as you like it:

.data : {      *(.rawdata*)  } 

You can also take a look here here for a bit more detailed information about .S template.

like image 29
lmctl Avatar answered Oct 06 '22 12:10

lmctl