Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions sys/src/ape/cmd/dw2elf/dw2elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#define EHDR_SIZE 64
#define PHDR_SIZE 56
#define SHDR_SIZE 64
#define PAGE_SIZE 0x1000

/* ── Plan9 etype values (APExp cc.h with TBOOL=3 inserted) ────────────── */
#define P9_TVOID 0
Expand Down Expand Up @@ -401,9 +402,22 @@ int main(int argc, char *argv[])
data_vaddr = (text_vaddr + textsize + AOUT_INITRND - 1)
& ~(AOUT_INITRND - 1);

/* ── ELF file offset layout ── */
text_foff = EHDR_SIZE + 2 * PHDR_SIZE; /* 176 */
data_foff = text_foff + (long)textsize;
/* ── ELF file offset layout ──
* ELF requires p_offset ≡ p_vaddr (mod p_align).
* Pad after the ELF headers so text_foff ≡ text_vaddr (mod PAGE_SIZE),
* then pad between text and data for the same constraint on data. */
{
long base = EHDR_SIZE + 2 * PHDR_SIZE; /* 176 */
long tmod = (long)(text_vaddr % PAGE_SIZE);
long cmod = base % PAGE_SIZE;
text_foff = base + ((tmod - cmod + PAGE_SIZE) % PAGE_SIZE);
}
{
long base = text_foff + (long)textsize;
long dmod = (long)(data_vaddr % PAGE_SIZE);
long cmod = base % PAGE_SIZE;
data_foff = base + ((dmod - cmod + PAGE_SIZE) % PAGE_SIZE);
}

/* ── DWARF generation ── */
/* sections 0=NULL 1=.text 2=.data 3=.bss → DWARF starts at 4 */
Expand All @@ -416,12 +430,15 @@ int main(int argc, char *argv[])
int res;

res = dwarf_producer_init(
DW_DLC_POINTER64 | DW_DLC_TARGET_LITTLEENDIAN,
DW_DLC_POINTER64 | DW_DLC_TARGET_LITTLEENDIAN |
DW_DLC_SYMBOLIC_RELOCATIONS,
dw_section_callback, NULL, NULL, NULL,
"x86_64", "V4", "",
&dbg, &derr);

if(res == DW_DLV_OK) {
if(res != DW_DLV_OK) {
fprintf(stderr, "dw2elf: dwarf_producer_init failed\n");
} else {
Comment on lines +439 to +441

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When dwarf_producer_init fails, it's helpful to include the error details provided by libdwarf. You can use dwarf_errmsg(derr) to get a human-readable description of the error.

		if(res != DW_DLV_OK) {
			fprintf(stderr, "dw2elf: dwarf_producer_init failed: %s\n", dwarf_errmsg(derr));
		} else {

for(i = 0; i < nsidecars; i++) {
size_t sclen;
uint8_t *scbuf = loadfile(sidecars[i], &sclen);
Expand Down Expand Up @@ -500,14 +517,19 @@ int main(int argc, char *argv[])
/* ── program headers ── */
write_phdr(ofd, PT_LOAD, PF_R|PF_X,
(uint64_t)text_foff, text_vaddr,
textsize, textsize, AOUT_INITRND);
textsize, textsize, PAGE_SIZE);
write_phdr(ofd, PT_LOAD, PF_R|PF_W,
(uint64_t)data_foff, data_vaddr,
datsize, (uint64_t)datsize + bsssize, AOUT_INITRND);
datsize, (uint64_t)datsize + bsssize, PAGE_SIZE);

/* ── text and data ── */
/* ── padding + text + padding + data ── */
cur = (long)(EHDR_SIZE + 2 * PHDR_SIZE);
while(cur < text_foff) { xwrite(ofd, &zero, 1); cur++; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Writing padding byte-by-byte using xwrite is inefficient as it results in a system call for every single byte. Since the maximum padding is PAGE_SIZE, it's better to write the padding in a single call using a zeroed buffer.

	if(cur < text_foff) {
		static char zeros[PAGE_SIZE];
		xwrite(ofd, zeros, text_foff - cur);
		cur = text_foff;
	}

xwrite(ofd, aout_buf + AOUT_HEADR, textsize);
cur += (long)textsize;
while(cur < data_foff) { xwrite(ofd, &zero, 1); cur++; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the text section padding, writing the data section padding byte-by-byte is inefficient. Consider using a single xwrite call with a zeroed buffer.

	if(cur < data_foff) {
		static char zeros[PAGE_SIZE];
		xwrite(ofd, zeros, data_foff - cur);
		cur = data_foff;
	}

xwrite(ofd, aout_buf + AOUT_HEADR + textsize, datsize);
cur += (long)datsize;

/* ── DWARF sections ── */
for(i = 0; i < n_dw_sects; i++) {
Expand Down
Loading