1

I need to put some externally generated data into an array inside a C program at compile time & to guarantee the array is 64-bit aligned. Specifically I want to initialise the input pool in the Linux random(4) device with data from /dev/urandom on the machine that compiles the kernel.

I already have a program that does it by generating a C header file that declares an array of unit64_t and fills it. That works fine in my test environment, but the kernel has large complicated makefiles which I do not fully understand & I'd like to avoid messing with them any more than absolutely necessary.

The makefile changes would be smaller if I could just generate a .o file & let the linker do the rest. A method using ld(1) is given here: Embedding resources in executable using GCC

That method also works fine in my tests, but I have two questions:

I need 64-bit alignment and looking at the manual it is not obvious that ld(1) guarantees that, or how to force it to if it does not.

The file size seems extravagant. I have 640 bytes of data but ld(1) gives me an output file that's about 5k. So far, playing with ld(1) options has not helped.

Sandy
  • 61
  • 2
  • You can use `objdump` to see what's using all that space in the output file. – Barmar Sep 11 '21 at 05:34
  • @Sandy - Your question is incorrectly worded - you want to put the data in after compile time, at link time. – Armali Sep 17 '21 at 17:51

1 Answers1

0

I need 64-bit alignment and looking at the manual it is not obvious that ld(1) guarantees that, or how to force it to if it does not.

Probably it does not. You can use the mentioned objdump also to see this:

~$ objdump -h binary.o 

binary.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .data         00000280  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, ALLOC, LOAD, DATA

Here the alignment is 20 = 1 byte = 8 bit.

If you use objcopy instead of ld to generate the object file, you can specify the needed alignment:

~$ objcopy -Ibinary -Oelf64-x86-64 --set-section-alignment .data=8 foo.bar binary.o 
~$ objdump -h binary.o 

binary.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .data         00000280  0000000000000000  0000000000000000  00000040  2**3
                  CONTENTS, ALLOC, LOAD, DATA
Armali
  • 18,255
  • 14
  • 57
  • 171