0

I'm trying to revive some old code that links text files (.glsl etc.) into an executable. With my current computer & Kubuntu OS, compiling in 64 bits, I can't read size information anymore. I found a simple example that fails for me in the same way at How do I add contents of text file as a section in an ELF file? . It is further simplified below.

myfile.txt:

Annon edhellon, edro hi ammen
Fennas nogothrim, lasto beth lammen

Objectified with, as in the example,

objcopy --input binary --output elf64-x86-64 --binary-architecture i386:x86-64 --rename-section .data=.rodata,CONTENTS,ALLOC,LOAD,READONLY,DATA myfile.txt myfile.o

I also tried ld -r -b binary -o myfile.o myfile.txt with the same result.

This is my main.c,

#include <stdlib.h>
#include <stdio.h>

/* These are external references to the symbols created by OBJCOPY */
extern char _binary_myfile_txt_start[];
extern char _binary_myfile_txt_end[];
extern char _binary_myfile_txt_size[];

int main() {
    char *data_start     = _binary_myfile_txt_start;
    char *data_end       = _binary_myfile_txt_end;
    size_t data_size  = (size_t)_binary_myfile_txt_size;

    printf ("data_start %p\n", data_start);
    printf ("data_end   %p\n", data_end);
    printf ("data_size  %zu\n", data_size);
}

compiled with gcc main.c myfile.o

When I run the code, the result is as follows:

data_start 0x55cd23b88032
data_end   0x55cd23b88074
data_size  94339555942466

The start and end pointers work, but data_size is nonsense. I'd expect it to be 66, as shown by wc. I've tried many obvious things but nothing seems to work.

  • Given `extern char _binary_myfile_txt_size[];`, `size_t data_size = (size_t)_binary_myfile_txt_size;` converts the *address* of the `_binary_myfile_txt_size` array to a `size_t`. `94339555942466` is `0x55CD23B86042` in hex. – Andrew Henle Aug 14 '19 at 11:17
  • I figured I get the right value by ```size_t dsize = (uint8_t)_binary_myfile_txt_size;```, but only if my file size fits into one byte. I don't understand why this works and why it doesn't work with, say, uint16_t or uint32_t. If I put uint64_t, I get the address again. – Jonni Lehtiranta Aug 14 '19 at 11:55

0 Answers0