31

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 = .;
}
HTTP 410
  • 17,300
  • 12
  • 76
  • 127
Pandemonium
  • 421
  • 1
  • 4
  • 7

4 Answers4

24

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.

cubuspl42
  • 7,833
  • 4
  • 41
  • 65
CesarB
  • 43,947
  • 7
  • 63
  • 86
  • 3
    The `bfdarch` text is not meant literally (and is not needed). For confusing see [Linking arbitrary data using gcc arm toolchain](http://stackoverflow.com/questions/17265950/linking-arbitrary-data-using-gcc-arm-toolchain). – artless noise Jun 24 '13 at 14:06
19

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.

Community
  • 1
  • 1
lmctl
  • 301
  • 3
  • 4
7

Another way with just the linker is to use TARGET and INPUT commands.

    TARGET(binary)
    INPUT (./DATA.bin)
    INPUT (./CODE.bin)
    # add more binary files here if needed
    
    OUTPUT_FORMAT("elf32-i386")
    
    ENTRY(start)
    
    SECTIONS
    {
        .text 0x100000 : {
            *(.multiboot)
            *(.text)
            *(.code)
            ./CODE.bin
            *(.rodata*)
        }
        .data : {
            *(.data)
        }
        .bss : {
            *(.bss)
        }
        kernel_end = .;
        roottask_start = .;
        .data : {
    
            ./DATA.bin
    
        }
        roottask_end = .;
    }

Additionally, you may need alignment stanzas around the binary input.

artless noise
  • 21,212
  • 6
  • 68
  • 105
Gerard
  • 81
  • 1
  • 1
7

Another solution is to generate a second linker script containing bytes from your binary using the BYTE function, and then include it in the former linker script using the INCLUDE directive.

You can generate the script from your binary file using hexdump in the following way:

cat ramelfs | hexdump -v -e '"BYTE(0x" 1/1 "%02X" ")\n"' > ramelfs.ld

And then include it this way :

SECTIONS {
    .text : {

        /* ... */

        kramelfs = .;
        INCLUDE "ramelfs.ld" ;
        kramelfs_end = .;

       /* ... */
    }
}
aimxhaisse
  • 71
  • 1
  • 1