Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22091

Objcopy symbols are mixed or invalid in executable

$
0
0

As a simple example of my problem, let's say we have two data arrays to embed into an executable to be used in a C program: chars and shorts. These data arrays are stored on disk as chars.raw and shorts.raw.

Using objcopy I can create object files that contain the data.

objcopy --input binary --output elf64-x86-64 chars.raw char_data.oobjcopy --input binary --output elf64-x86-64 shorts.raw short_data.o

objdump shows that the data is correctly stored and exported as _binary_chars_raw_start, end, and size.

$ objdump -x char_data.o char_data.o:     file format elf64-x86-64char_data.oarchitecture: i386:x86-64, flags 0x00000010:HAS_SYMSstart address 0x0000000000000000Sections:Idx Name          Size      VMA               LMA               File off  Algn  0 .data         0000000e  0000000000000000  0000000000000000  00000040  2**0                  CONTENTS, ALLOC, LOAD, DATASYMBOL TABLE:0000000000000000 l    d  .data  0000000000000000 .data0000000000000000 g       .data  0000000000000000 _binary_chars_raw_start000000000000000e g       .data  0000000000000000 _binary_chars_raw_end000000000000000e g       *ABS*  0000000000000000 _binary_chars_raw_size

(Similar output for short_data.o)

However, when I link these object files with my code into an executable, I run into problems. For example:

#include <stdio.h>extern char _binary_chars_raw_start[];extern char _binary_chars_raw_end[];extern int _binary_chars_raw_size;extern short _binary_shorts_raw_start[];extern short _binary_shorts_raw_end[];extern int _binary_shorts_raw_size;int main(int argc, char **argv) {        printf("%ld == %ld\n", _binary_chars_raw_end - _binary_chars_raw_start, _binary_chars_raw_size / sizeof(char));        printf("%ld == %ld\n", _binary_shorts_raw_end - _binary_shorts_raw_start, _binary_shorts_raw_size / sizeof(short));}

(compiled with gcc main.c char_data.o short_data.o -o main) prints

14 == 1966087 == 98304

on my computer. The size _binary_chars_raw_size (and short) is not correct and I don't know why.

Similarly, if the _starts or _ends are used to initialize anything, then they may not even be located near each other in the executable (_end - _start is not equal to the size, and may even be negative).

What am I doing wrong?


Viewing all articles
Browse latest Browse all 22091

Trending Articles