Skip to content

Commit 7c38ea7

Browse files
author
Joshua Warner
committed
give thunks symbol names in the bootimage build, use Heap::allocate instead of malloc
1 parent 4266b0d commit 7c38ea7

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

src/binaryToObject/tools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SymbolInfo {
5555
unsigned addr;
5656
String name;
5757

58-
inline SymbolInfo(uint64_t addr, const char* name):
58+
inline SymbolInfo(uint64_t addr, const String& name):
5959
addr(addr),
6060
name(name) {}
6161

src/bootimage.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,28 +1291,32 @@ writeBootImage2(Thread* t, OutputStream* bootimageOutput, OutputStream* codeOutp
12911291

12921292
class MyCompilationHandler : public Processor::CompilationHandler {
12931293
public:
1294-
virtual void compiled(const void* code, unsigned size UNUSED, unsigned frameSize UNUSED, const char* class_, const char* name, const char* spec) {
1295-
if (class_ and name and spec) {
1296-
size_t classLen = strlen(class_);
1297-
size_t nameLen = strlen(name);
1298-
size_t specLen = strlen(spec);
1299-
1300-
char* completeName = (char*)malloc(classLen + nameLen + specLen + 2);
1301-
sprintf(completeName, "%s.%s%s", class_, name, spec);
1302-
uint64_t offset = reinterpret_cast<uint64_t>(code) - codeOffset;
1303-
symbols.add(SymbolInfo(offset, completeName));
1304-
// printf("%ld %ld %s.%s%s\n", offset, offset + size, class_, name, spec);
1305-
}
1294+
1295+
String heapDup(const char* name) {
1296+
String ret(name);
1297+
char* n = (char*)heap->allocate(ret.length + 1);
1298+
memcpy(n, ret.text, ret.length + 1);
1299+
ret.text = n;
1300+
return ret;
1301+
}
1302+
1303+
virtual void compiled(const void* code, unsigned size UNUSED, unsigned frameSize UNUSED, const char* name) {
1304+
uint64_t offset = reinterpret_cast<uint64_t>(code) - codeOffset;
1305+
symbols.add(SymbolInfo(offset, heapDup(name)));
1306+
// printf("%ld %ld %s.%s%s\n", offset, offset + size, class_, name, spec);
13061307
}
13071308

13081309
virtual void dispose() {}
13091310

13101311
DynamicArray<SymbolInfo> symbols;
13111312
uint64_t codeOffset;
1313+
Heap* heap;
1314+
1315+
MyCompilationHandler(uint64_t codeOffset, Heap* heap):
1316+
codeOffset(codeOffset),
1317+
heap(heap) {}
13121318

1313-
MyCompilationHandler(uint64_t codeOffset):
1314-
codeOffset(codeOffset) {}
1315-
} compilationHandler(reinterpret_cast<uint64_t>(code));
1319+
} compilationHandler(reinterpret_cast<uint64_t>(code), t->m->heap);
13161320

13171321
t->m->processor->addCompilationHandler(&compilationHandler);
13181322

@@ -1660,13 +1664,13 @@ writeBootImage2(Thread* t, OutputStream* bootimageOutput, OutputStream* codeOutp
16601664

16611665
platform->writeObject(bootimageOutput, Slice<SymbolInfo>(bootimageSymbols, 2), Slice<const uint8_t>(bootimageData.data, bootimageData.length), Platform::Writable, TargetBytesPerWord);
16621666

1663-
compilationHandler.symbols.add(SymbolInfo(0, strdup("_binary_codeimage_bin_start")));
1664-
compilationHandler.symbols.add(SymbolInfo(image->codeSize, strdup("_binary_codeimage_bin_end")));
1667+
compilationHandler.symbols.add(SymbolInfo(0, "_binary_codeimage_bin_start"));
1668+
compilationHandler.symbols.add(SymbolInfo(image->codeSize, "_binary_codeimage_bin_end"));
16651669

16661670
platform->writeObject(codeOutput, Slice<SymbolInfo>(compilationHandler.symbols), Slice<const uint8_t>(code, image->codeSize), Platform::Executable, TargetBytesPerWord);
16671671

1668-
for(SymbolInfo* sym = compilationHandler.symbols.begin(); sym != compilationHandler.symbols.end(); sym++) {
1669-
free(const_cast<void*>((const void*)sym->name.text));
1672+
for(SymbolInfo* sym = compilationHandler.symbols.begin(); sym != compilationHandler.symbols.end() - 2; sym++) {
1673+
t->m->heap->free(const_cast<void*>((const void*)sym->name.text), sym->name.length + 1);
16701674
}
16711675
}
16721676
}

src/compile.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9087,6 +9087,20 @@ class MyProcessor: public Processor {
90879087
CompilationHandlerList* compilationHandlers;
90889088
};
90899089

9090+
const char*
9091+
stringOrNull(const char* str) {
9092+
if(str) {
9093+
return str;
9094+
} else {
9095+
return "(null)";
9096+
}
9097+
}
9098+
9099+
size_t
9100+
stringOrNullSize(const char* str) {
9101+
return strlen(stringOrNull(str));
9102+
}
9103+
90909104
void
90919105
logCompile(MyThread* t, const void* code, unsigned size, const char* class_,
90929106
const char* name, const char* spec)
@@ -9108,9 +9122,15 @@ logCompile(MyThread* t, const void* code, unsigned size, const char* class_,
91089122
class_, name, spec);
91099123
}
91109124

9125+
size_t nameLength = stringOrNullSize(class_) + stringOrNullSize(name) + stringOrNullSize(spec) + 2;
9126+
9127+
THREAD_RUNTIME_ARRAY(t, char, completeName, nameLength);
9128+
9129+
sprintf(RUNTIME_ARRAY_BODY(completeName), "%s.%s%s", stringOrNull(class_), stringOrNull(name), stringOrNull(spec));
9130+
91119131
MyProcessor* p = static_cast<MyProcessor*>(t->m->processor);
91129132
for(CompilationHandlerList* h = p->compilationHandlers; h; h = h->next) {
9113-
h->handler->compiled(code, 0, 0, class_, name, spec);
9133+
h->handler->compiled(code, 0, 0, RUNTIME_ARRAY_BODY(completeName));
91149134
}
91159135
}
91169136

src/processor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Processor {
4343

4444
class CompilationHandler {
4545
public:
46-
virtual void compiled(const void* code, unsigned size, unsigned frameSize, const char* class_, const char* name, const char* spec) = 0;
46+
virtual void compiled(const void* code, unsigned size, unsigned frameSize, const char* name) = 0;
4747

4848
virtual void dispose() = 0;
4949
};

0 commit comments

Comments
 (0)