Skip to content

Commit d539a71

Browse files
committed
elf: add debug statement aiming to help diagnose problems
Signed-off-by: Waldemar Kozaczuk <jwkozaczuk@gmail.com>
1 parent e322059 commit d539a71

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

core/elf.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030

3131
#include "arch.hh"
3232

33+
#define ELF_DEBUG_ENABLED 0
34+
35+
#if ELF_DEBUG_ENABLED
36+
#define elf_debug(format,...) kprintf("ELF [tid:%d, %s]: " format, sched::thread::current()->id(), _pathname.c_str(), ##__VA_ARGS__)
37+
#else
38+
#define elf_debug(...)
39+
#endif
40+
3341
TRACEPOINT(trace_elf_load, "%s", const char *);
3442
TRACEPOINT(trace_elf_unload, "%s", const char *);
3543
TRACEPOINT(trace_elf_lookup, "%s", const char *);
@@ -116,10 +124,12 @@ object::object(program& prog, std::string pathname)
116124
, _is_executable(false)
117125
, _visibility(nullptr)
118126
{
127+
elf_debug("Instantiated\n");
119128
}
120129

121130
object::~object()
122131
{
132+
elf_debug("Removed\n");
123133
_prog.free_dtv(this);
124134
}
125135

@@ -327,6 +337,7 @@ void object::set_base(void* base)
327337
}
328338

329339
_end = _base + q->p_vaddr + q->p_memsz;
340+
elf_debug("The base set to: 0x%016x and end: 0x%016x\n", _base, _end);
330341
}
331342

332343
void* object::base() const
@@ -367,6 +378,7 @@ void file::load_segment(const Elf64_Phdr& phdr)
367378
mmu::map_anon(_base + vstart + filesz, memsz - filesz, flag, perm);
368379
}
369380
}
381+
elf_debug("Loaded and mapped PT_LOAD segment at: 0x%016x of size: 0x%x\n", _base + vstart, filesz); //TODO: Add memory?
370382
}
371383

372384
bool object::mlocked()
@@ -407,6 +419,7 @@ Elf64_Note::Elf64_Note(void *_base, char *str)
407419
extern "C" char _pie_static_tls_start, _pie_static_tls_end;
408420
void object::load_segments()
409421
{
422+
elf_debug("Loading segments\n");
410423
for (unsigned i = 0; i < _ehdr.e_phnum; ++i) {
411424
auto &phdr = _phdrs[i];
412425
switch (phdr.p_type) {
@@ -462,6 +475,7 @@ void object::load_segments()
462475
_tls_init_size = phdr.p_filesz;
463476
_tls_uninit_size = phdr.p_memsz - phdr.p_filesz;
464477
_tls_alignment = phdr.p_align;
478+
elf_debug("Found TLS segment at 0x%016x of aligned size: %x\n", _tls_segment, get_aligned_tls_size());
465479
break;
466480
default:
467481
abort("Unknown p_type in executable %s: %d\n", pathname(), phdr.p_type);
@@ -693,6 +707,7 @@ void object::relocate_rela()
693707
abort();
694708
}
695709
}
710+
elf_debug("Relocated %d symbols in DT_RELA\n", nb);
696711
}
697712

698713
extern "C" { void __elf_resolve_pltgot(void); }
@@ -741,6 +756,7 @@ void object::relocate_pltgot()
741756
*static_cast<u64*>(addr) += reinterpret_cast<u64>(_base);
742757
}
743758
}
759+
elf_debug("Relocated %d PLT symbols in DT_JMPREL\n", nrel);
744760

745761
// PLTGOT resolution has a special calling convention,
746762
// for x64 the symbol index and some word is pushed on the stack,
@@ -987,6 +1003,7 @@ void object::load_needed(std::vector<std::shared_ptr<object>>& loaded_objects)
9871003
}
9881004
auto needed = dynamic_str_array(DT_NEEDED);
9891005
for (auto lib : needed) {
1006+
elf_debug("Loading DT_NEEDED object: %s \n", lib);
9901007
auto obj = _prog.load_object(lib, rpath, loaded_objects);
9911008
if (obj) {
9921009
// Keep a reference to the needed object, so it won't be
@@ -1000,6 +1017,7 @@ void object::load_needed(std::vector<std::shared_ptr<object>>& loaded_objects)
10001017

10011018
void object::unload_needed()
10021019
{
1020+
elf_debug("Unloading object dependent objects \n");
10031021
_used_by_resolve_plt_got.clear();
10041022
while (!_needed.empty()) {
10051023
_needed.pop_back();
@@ -1049,15 +1067,19 @@ void object::run_init_funcs(int argc, char** argv)
10491067
if (dynamic_exists(DT_INIT)) {
10501068
auto func = dynamic_ptr<void>(DT_INIT);
10511069
if (func) {
1070+
elf_debug("Executing DT_INIT function\n");
10521071
reinterpret_cast<void(*)(int, char**)>(func)(argc, argv);
1072+
elf_debug("Finished executing DT_INIT function\n");
10531073
}
10541074
}
10551075
if (dynamic_exists(DT_INIT_ARRAY)) {
10561076
auto funcs = dynamic_ptr<void(*)(int, char**)>(DT_INIT_ARRAY);
10571077
auto nr = dynamic_val(DT_INIT_ARRAYSZ) / sizeof(*funcs);
1078+
elf_debug("Executing %d DT_INIT_ARRAYSZ functions\n", nr);
10581079
for (auto i = 0u; i < nr; ++i) {
10591080
funcs[i](argc, argv);
10601081
}
1082+
elf_debug("Finished executing %d DT_INIT_ARRAYSZ functions\n", nr);
10611083
}
10621084
}
10631085

@@ -1067,14 +1089,17 @@ void object::run_fini_funcs()
10671089
if (dynamic_exists(DT_FINI_ARRAY)) {
10681090
auto funcs = dynamic_ptr<void (*)()>(DT_FINI_ARRAY);
10691091
auto nr = dynamic_val(DT_FINI_ARRAYSZ) / sizeof(*funcs);
1092+
elf_debug("Executing %d DT_FINI_ARRAYSZ functions\n", nr);
10701093
// According to the standard, call functions in reverse order.
10711094
for (int i = nr - 1; i >= 0; --i) {
10721095
funcs[i]();
10731096
}
1097+
elf_debug("Finished executing %d DT_FINI_ARRAYSZ functions\n", nr);
10741098
}
10751099
if (dynamic_exists(DT_FINI)) {
10761100
auto func = dynamic_ptr<void>(DT_FINI);
10771101
if (func) {
1102+
elf_debug("Executing DT_FINI function\n");
10781103
reinterpret_cast<void(*)()>(func)();
10791104
}
10801105
}
@@ -1096,6 +1121,7 @@ void object::alloc_static_tls()
10961121
if (!_static_tls && tls_size) {
10971122
_static_tls = true;
10981123
_static_tls_offset = _static_tls_alloc.fetch_add(tls_size, std::memory_order_relaxed);
1124+
elf_debug("Allocated static TLS at 0x%016x of size: 0x%x\n", _static_tls_offset, tls_size);
10991125
}
11001126
}
11011127

@@ -1131,10 +1157,12 @@ void object::init_static_tls()
11311157
}
11321158
if (obj->is_executable()) {
11331159
obj->prepare_local_tls(_initial_tls_offsets);
1160+
elf_debug("Initialized local-exec static TLS for %s\n", obj->pathname().c_str());
11341161
}
11351162
else {
11361163
obj->prepare_initial_tls(_initial_tls.get(), _initial_tls_size,
11371164
_initial_tls_offsets);
1165+
elf_debug("Initialized initial-exec static TLS for %s of size: 0x%x\n", obj->pathname().c_str(), _initial_tls_size);
11381166
}
11391167
}
11401168
}
@@ -1712,6 +1740,7 @@ struct module_and_offset {
17121740

17131741
char *object::setup_tls()
17141742
{
1743+
elf_debug("Setting up dynamic TLS of %d bytes\n", _tls_init_size + _tls_uninit_size);
17151744
return (char *) sched::thread::current()->setup_tls(
17161745
_module_index, _tls_segment, _tls_init_size, _tls_uninit_size);
17171746
}

0 commit comments

Comments
 (0)