summaryrefslogtreecommitdiff
path: root/bxgen.c
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-07-12 07:04:33 +0200
committerMitya Selivanov <automainint@guattari.tech>2024-07-12 07:04:33 +0200
commitda1b4ac8ed13a8a013989f5f7ff56639cf594d46 (patch)
treeaa4d2788ad07ca0dbab5c7a2520a2f2953f762e4 /bxgen.c
parent0b051356031a7b446f3f97ffff3a2aca5601c5a9 (diff)
downloadbxgen-da1b4ac8ed13a8a013989f5f7ff56639cf594d46.zip
Update relocations
Diffstat (limited to 'bxgen.c')
-rwxr-xr-xbxgen.c77
1 files changed, 74 insertions, 3 deletions
diff --git a/bxgen.c b/bxgen.c
index 33a6908..9d6781c 100755
--- a/bxgen.c
+++ b/bxgen.c
@@ -779,6 +779,12 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
// AR https://man.freebsd.org/cgi/man.cgi?query=ar&sektion=5
// ELF https://man7.org/linux/man-pages/man5/elf.5.html
//
+ // Relocation types
+ // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/
+ // https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter7-2/index.html
+ //
+ // https://web.archive.org/web/20150324024617/http://mylinuxbook.com/readelf-command/
+ //
// LLVM impl https://github.com/llvm/llvm-project/blob/main/lld/ELF/Driver.cpp#L2822
// https://github.com/llvm/llvm-project/blob/main/lld/ELF/Writer.cpp#L304
// https://github.com/llvm/llvm-project/blob/main/lld/ELF/OutputSections.cpp#L469
@@ -1315,10 +1321,9 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
switch (type) {
// ================================================================
//
- // Symbols and dynamic linking symbols
+ // Symbols
- case 2:
- case 6: {
+ case 2: {
// Find symbol addresses
//
@@ -1404,6 +1409,8 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
}[sym_info & 0xf] : ""
);
+ printf("%-3d", sym_shndx);
+
if (sym_size != 0)
printf("- %lld bytes", sym_size);
printf("\n");
@@ -1456,6 +1463,16 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
// ================================================================
//
// Relocarions with addends
+ //
+ // for .rela.NAME:
+ //
+ // .NAME[rela_offset] <- calc_reloc(
+ // B = base_memory_address
+ // P = rela_offset
+ // A = rela_addent
+ // S = .symtab[rela_sym].sym_value
+ // Z = .symtab[rela_sym].sym_size
+ // )
case 4: {
BX_ASSERT(entsize == 24);
@@ -1482,6 +1499,60 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
printf(" ");
printf("%08llx sym %-2d type %-2d add %-2lld", rela_offset, rela_sym, rela_type, rela_addent);
+
+ // Check value from destination address
+ //
+ {
+ i64 prev_offset = current_offset;
+ i64 prev_byte_count = byte_count;
+
+ u64 sym_size = 0;
+
+ // Go to the symbol table
+ for (u64 j = 0; j < section_count; ++j) {
+ io_seek(f, begin_offset + section_header_offset + j * 64 + 4, IO_SEEK_BEGIN, io_user_data);
+ u32 type;
+ READ(type);
+
+ if (type != 2) continue;
+
+ io_seek(f, 16, IO_SEEK_CURSOR, io_user_data);
+ u64 offset;
+ READ(offset);
+
+ io_seek(f, begin_offset + offset + rela_sym * 24 + 16, IO_SEEK_BEGIN, io_user_data);
+ READ(sym_size);
+
+ break;
+ }
+
+ if (sym_size > 0) {
+ // NOTE Ad hok
+ // Go to the previous section
+ io_seek(f, begin_offset + section_header_offset + (i - 1) * 64 + 24, IO_SEEK_BEGIN, io_user_data);
+
+ u64 offset;
+ u64 size;
+ READ(offset);
+ READ(size);
+
+ if (size > 0) {
+ io_seek(f, begin_offset + offset + rela_offset, IO_SEEK_BEGIN, io_user_data);
+ static u8 buf[4];
+ if (sym_size > 4)
+ sym_size = 4;
+ io_read(f, sym_size, buf, io_user_data);
+
+ for (u32 k = 0; k < sym_size; ++k)
+ BX_ASSERT(buf[k] == 0);
+ }
+ }
+
+ io_seek(f, prev_offset, IO_SEEK_BEGIN, io_user_data);
+ current_offset = prev_offset;
+ byte_count = prev_byte_count;
+ }
+
printf("\n");
}