summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-07-10 04:18:12 +0200
committerMitya Selivanov <automainint@guattari.tech>2024-07-10 04:18:12 +0200
commit342e1ae4cc7ca25c269f59ef4d2591d1b89862da (patch)
treea2d8df5d1bb6301a9501a3b231bb313f1582f26f
parent94725fda14c32b4c76b2f482049f71928063eec0 (diff)
downloadbxgen-342e1ae4cc7ca25c269f59ef4d2591d1b89862da.zip
Decoding symbols
-rwxr-xr-xbxgen.c126
1 files changed, 112 insertions, 14 deletions
diff --git a/bxgen.c b/bxgen.c
index 3decce1..efb38d4 100755
--- a/bxgen.c
+++ b/bxgen.c
@@ -663,7 +663,7 @@ void unit_proc_remove(Pool *pool, i64 unit, i64 proc) {
Proc *p = &pool->entities[proc].proc;
// TODO
- // Implement large entities.
+ // Implement large entities.
BX_ASSERT(p->index_in_unit != UNDEFINED);
BX_ASSERT(u->procs[p->index_in_unit] == proc);
@@ -1081,6 +1081,10 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u16 strings_index;
u64 strings_offset;
+ u64 symbol_names_offset;
+ u32 symbol_names_size;
+ b8 symbol_names_found = 0;
+
// ELF header
//
{
@@ -1123,10 +1127,10 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u64 section_offset = section_header_offset - (current_offset - begin_offset);
io_seek(f, section_offset, IO_SEEK_CURSOR, io_user_data);
- byte_count -= section_offset;
+ byte_count -= section_offset;
current_offset += section_offset;
- // Find offset to the string table data
+ // Find offset to the section name string table data
//
{
i64 prev_offset = current_offset;
@@ -1135,9 +1139,52 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
IO_SEEK_BEGIN, io_user_data);
n = io_read(f, 8, &strings_offset, io_user_data); if (n == 0) break;
-
+
+ io_seek(f, prev_offset, IO_SEEK_BEGIN, io_user_data);
+ current_offset = prev_offset;
+ }
+
+ // Find offset to the symbol string table data
+ //
+ {
+ i64 prev_offset = current_offset;
+ i64 prev_byte_count = byte_count;
+
+ for (u16 i = 0; i < section_count; ++i) {
+ io_seek(f,
+ begin_offset + section_header_offset + i * 64,
+ IO_SEEK_BEGIN, io_user_data);
+ u32 name;
+ u64 offset;
+ u32 size;
+
+ READ(name);
+ io_seek(f, 20, IO_SEEK_CURSOR, io_user_data);
+ READ(offset);
+ READ(size);
+
+ // Search for the name in the string table
+ //
+
+ io_seek(f,
+ begin_offset + strings_offset + name,
+ IO_SEEK_BEGIN, io_user_data);
+
+ c8 buf[8];
+ n = io_read(f, sizeof buf, buf, io_user_data); if (n == 0) break;
+
+ if (!bx_str_eq(buf, ".strtab"))
+ continue;
+
+ symbol_names_offset = offset;
+ symbol_names_size = size;
+ symbol_names_found = 1;
+ break;
+ }
+
io_seek(f, prev_offset, IO_SEEK_BEGIN, io_user_data);
current_offset = prev_offset;
+ byte_count = prev_byte_count;
}
}
@@ -1189,11 +1236,11 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
begin_offset + strings_offset + name,
IO_SEEK_BEGIN, io_user_data);
- i32 m = 50;
+ i32 padding = 50;
printf(" ");
- for (;; --m) {
+ for (;; --padding) {
c8 c;
n = io_read(f, 1, &c, io_user_data); if (n == 0) break;
if (c == '\0')
@@ -1201,8 +1248,9 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
printf("%c", c);
}
- printf("%*s", m, "");
-
+ if (padding > 0)
+ printf("%*s", padding, "");
+
io_seek(f, prev_offset, IO_SEEK_BEGIN, io_user_data);
current_offset = prev_offset;
}
@@ -1257,8 +1305,13 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
io_seek(f,
begin_offset + offset,
IO_SEEK_BEGIN, io_user_data);
+ current_offset = begin_offset + offset;
+
+ printf("\n");
for (byte_count = size; byte_count > 0;) {
+ BX_ASSERT(symbol_names_found);
+
u32 sym_name;
u8 sym_info;
u8 sym_other;
@@ -1273,23 +1326,67 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
READ(sym_value); BX_ASSERT(n != 0);
READ(sym_size); BX_ASSERT(n != 0);
- printf(" %-4d %-5lld", sym_shndx, sym_value);
-
+ printf(" ");
+
+ if (sym_name != 0) {
+ if (sym_name < symbol_names_size) {
+ // Search for the symbol name in the string table
+ //
+
+ i64 prev_offset = current_offset;
+ io_seek(f,
+ begin_offset + symbol_names_offset + sym_name,
+ IO_SEEK_BEGIN, io_user_data);
+
+ i32 padding = 48;
+
+ if ((sym_info & 0xf) == 1 ||
+ (sym_info & 0xf) == 2)
+ printf("%s", "\x1b[32m");
+
+ printf("\"");
+
+ for (;; --padding) {
+ c8 c;
+ n = io_read(f, 1, &c, io_user_data); if (n == 0) break;
+ if (c == '\0')
+ break;
+ printf("%c", c);
+ }
+
+ printf("\"");
+ if ((sym_info & 0xf) == 1 ||
+ (sym_info & 0xf) == 2)
+ printf("%s", "\x1b[37m");
+ if (padding > 0)
+ printf("%*s", padding, "");
+
+ io_seek(f, prev_offset, IO_SEEK_BEGIN, io_user_data);
+ current_offset = prev_offset;
+ } else
+ printf("%-50d", sym_name);
+ } else
+ printf("%*s", 50, "");
+
+ printf("%08llx ", sym_value);
+
printf("%-8s ",
(sym_info & 0xf) <= 4 ? (c8 const *[]) {
"No type",
- "Object",
+ "Data",
"Func",
"Section",
"File",
}[sym_info & 0xf] : ""
);
-
+
if (sym_size != 0)
printf("- %lld bytes", sym_size);
printf("\n");
}
+ printf("\n");
+
io_seek(f, prev_offset, IO_SEEK_BEGIN, io_user_data);
current_offset = prev_offset;
byte_count = prev_byte_count;
@@ -1375,7 +1472,8 @@ void bx_assert(b8 condition, c8 const *message, u32 line, c8 const *file) {
if (condition)
return;
- fprintf(stderr, "\x1b[31mASSERTION:\x1b[37m `\x1b[33m%s\x1b[37m` is false in \x1b[36m%s:%d\x1b[37m\n", message, file, line);
+ fflush(stdout);
+ fprintf(stderr, "\r\x1b[31mASSERTION:\x1b[37m `\x1b[33m%s\x1b[37m` is false in \x1b[36m%s:%d\x1b[37m\n", message, file, line);
exit(-1);
}
@@ -1571,7 +1669,7 @@ int main(int argc, char **argv) {
printf("\nBye!\n");
return 0;
-}
+}
#endif