Skip to content

Commit 4c58d21

Browse files
authored
Merge pull request #198 from DrXiao/refine-elf-gen
Improve the ELF generation process
2 parents 411a6f6 + 46f7b1f commit 4c58d21

File tree

7 files changed

+385
-258
lines changed

7 files changed

+385
-258
lines changed

src/arm-codegen.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void cfg_flatten()
179179

180180
void emit(int code)
181181
{
182-
elf_write_code_int(code);
182+
elf_write_int(elf_code, code);
183183
}
184184

185185
void emit_ph2_ir(ph2_ir_t *ph2_ir)
@@ -271,16 +271,16 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
271271
emit(__teq(rn));
272272
if (ph2_ir->is_branch_detached) {
273273
emit(__b(__NE, 8));
274-
emit(__b(__AL, ph2_ir->else_bb->elf_offset - elf_code_idx));
274+
emit(__b(__AL, ph2_ir->else_bb->elf_offset - elf_code->size));
275275
} else
276-
emit(__b(__NE, ph2_ir->then_bb->elf_offset - elf_code_idx));
276+
emit(__b(__NE, ph2_ir->then_bb->elf_offset - elf_code->size));
277277
return;
278278
case OP_jump:
279-
emit(__b(__AL, ph2_ir->next_bb->elf_offset - elf_code_idx));
279+
emit(__b(__AL, ph2_ir->next_bb->elf_offset - elf_code->size));
280280
return;
281281
case OP_call:
282282
func = find_func(ph2_ir->func_name);
283-
emit(__bl(__AL, func->bbs->elf_offset - elf_code_idx));
283+
emit(__bl(__AL, func->bbs->elf_offset - elf_code->size));
284284
return;
285285
case OP_load_data_address:
286286
emit(__movw(__AL, rd, ph2_ir->src0 + elf_data_start));
@@ -436,7 +436,7 @@ void code_generate()
436436
emit(__movt(__AL, __r8, GLOBAL_FUNC->stack_size));
437437
emit(__sub_r(__AL, __sp, __sp, __r8));
438438
emit(__mov_r(__AL, __r12, __sp));
439-
emit(__bl(__AL, GLOBAL_FUNC->bbs->elf_offset - elf_code_idx));
439+
emit(__bl(__AL, GLOBAL_FUNC->bbs->elf_offset - elf_code->size));
440440

441441
/* exit */
442442
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
@@ -468,7 +468,7 @@ void code_generate()
468468
emit(__add_r(__AL, __r8, __r12, __r8));
469469
emit(__lw(__AL, __r0, __r8, 0));
470470
emit(__add_i(__AL, __r1, __r8, 4));
471-
emit(__b(__AL, MAIN_BB->elf_offset - elf_code_idx));
471+
emit(__b(__AL, MAIN_BB->elf_offset - elf_code->size));
472472

473473
for (int i = 0; i < ph2_ir_idx; i++) {
474474
ph2_ir = PH2_IR_FLATTEN[i];

src/defs.h

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ typedef struct {
351351

352352
typedef struct basic_block basic_block_t;
353353

354-
/* Definition of a dynamic array structure for sources in src/globals.c
354+
/* Definition of a growable buffer for a mutable null-terminated string
355355
* size: Current number of elements in the array
356356
* capacity: Number of elements that can be stored without resizing
357357
* elements: Pointer to the array of characters
@@ -360,7 +360,7 @@ typedef struct {
360360
int size;
361361
int capacity;
362362
char *elements;
363-
} source_t;
363+
} strbuf_t;
364364

365365
/* phase-2 IR definition */
366366
struct ph2_ir {
@@ -547,3 +547,48 @@ typedef struct {
547547
var_t *var;
548548
int polluted;
549549
} regfile_t;
550+
551+
/* FIXME: replace char[2] with a short data type in ELF header structures */
552+
/* ELF header */
553+
typedef struct {
554+
char e_ident[16];
555+
char e_type[2];
556+
char e_machine[2];
557+
int e_version;
558+
int e_entry;
559+
int e_phoff;
560+
int e_shoff;
561+
int e_flags;
562+
char e_ehsize[2];
563+
char e_phentsize[2];
564+
char e_phnum[2];
565+
char e_shentsize[2];
566+
char e_shnum[2];
567+
char e_shstrndx[2];
568+
} elf32_hdr_t;
569+
570+
/* ELF program header */
571+
typedef struct {
572+
int p_type;
573+
int p_offset;
574+
int p_vaddr;
575+
int p_paddr;
576+
int p_filesz;
577+
int p_memsz;
578+
int p_flags;
579+
int p_align;
580+
} elf32_phdr_t;
581+
582+
/* ELF section header */
583+
typedef struct {
584+
int sh_name;
585+
int sh_type;
586+
int sh_flags;
587+
int sh_addr;
588+
int sh_offset;
589+
int sh_size;
590+
int sh_link;
591+
int sh_info;
592+
int sh_addralign;
593+
int sh_entsize;
594+
} elf32_shdr_t;

0 commit comments

Comments
 (0)