This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

R_X86_64_COPY bug


Dear binutils developers,

We are seeing a issue with copy relocation semantics in ld.

A struct with zero length array is defined in a shared library.
Because of copy relocation semantics, any binary linking with that
library sees those elements as zero initialized.

We saw this bug also on ARM with R_ARM_COPY.

My binutils is binutils 2.23.1-3.

-Fredrick

The following program demonstrates the phenomenon.

test_lib.h:
#ifndef __TEST_MISC_H__
#define __TEST_MISC_H__
struct test_array {
int array_len;
int array[];
};

void print_array(struct test_array *);
#endif

test_lib.c:
#include "test_misc.h"
#include <stdio.h>
struct test_array test_dynamic = {
6,
{1, 2, 3, 4, 5, 6},
};

void
print_array(struct test_array *a) {
int i;
for (i = 0; i<a->array_len; i++) {
printf("%d\n", a->array[i]);
}
}

test_misc.c:
#include "test_misc.h"
extern struct test_array test_dynamic;
int
main() {
print_array(&test_dynamic);
return 0;
}

Makefile:
all: test_misc.bin

test_misc.bin: test_misc.o libtest.so
cc test_misc.o libtest.so -o test_misc.bin

libtest.so: test_lib.o
cc -shared -Wl,-soname,libtest.so -o libtest.so test_lib.o

test_lib.o: test_lib.c
cc -fPIC -o test_lib.o -c test_lib.c
.PHONY: clean
clean:
rm -rf test_misc.bin
rm -rf libtest.so
rm -rf *.o


$ LD_LIBRARY_PATH=. ./test_misc.bin
0
0
0
0
0
0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]