{
FILE *f;
int i, num;
- uint64_t rela_start, rela_end, text_base;
+ uint64_t rela_start, rela_end, text_base, file_size;
if (argc != 5) {
fprintf(stderr, "Statically apply ELF rela relocations\n");
return 3;
}
- if (rela_start > rela_end || rela_start < text_base ||
- (rela_end - rela_start) % sizeof(Elf64_Rela)) {
+ if (rela_start > rela_end || rela_start < text_base) {
fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
return 3;
}
rela_start -= text_base;
rela_end -= text_base;
+ fseek(f, 0, SEEK_END);
+ file_size = ftell(f);
+ rewind(f);
+
+ if (rela_end > file_size) {
+ // Most likely compiler inserted some section that didn't get
+ // objcopy-ed into the final binary
+ rela_end = file_size;
+ }
+
+ if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
+ fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
+ return 3;
+ }
+
num = (rela_end - rela_start) / sizeof(Elf64_Rela);
for (i = 0; i < num; i++) {