Skip to content

Commit

Permalink
Accept arbitrary name section
Browse files Browse the repository at this point in the history
Not restrict to ".text", ".rodata", ".data" and ".bss",
but also arbitrary name.
  • Loading branch information
tyfkda committed Aug 17, 2024
1 parent bb06c45 commit 070f154
Show file tree
Hide file tree
Showing 16 changed files with 595 additions and 418 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ cpp_SRCS:=$(wildcard $(CPP_DIR)/*.c) \
$(CC1_DIR)/lexer.c $(UTIL_DIR)/util.c $(UTIL_DIR)/table.c
as_SRCS:=$(wildcard $(AS_DIR)/*.c) \
$(wildcard $(AS_ARCH_DIR)/*.c) \
$(UTIL_DIR)/gen_section.c $(UTIL_DIR)/util.c $(UTIL_DIR)/elfutil.c $(UTIL_DIR)/table.c
$(UTIL_DIR)/util.c $(UTIL_DIR)/elfutil.c $(UTIL_DIR)/table.c
ld_SRCS:=$(wildcard $(LD_DIR)/*.c) $(UTIL_DIR)/archive.c \
$(UTIL_DIR)/gen_section.c $(UTIL_DIR)/util.c $(UTIL_DIR)/elfutil.c $(UTIL_DIR)/table.c

Expand Down
4 changes: 2 additions & 2 deletions src/as/arch/aarch64/asm_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void make_code32(Inst *inst, Code *code, unsigned int *buf, int len) {
code->len += len;
}

inline bool assemble_error(const ParseInfo *info, const char *message) {
inline bool assemble_error(ParseInfo *info, const char *message) {
parse_error(info, message);
return false;
}
Expand Down Expand Up @@ -588,7 +588,7 @@ static const AsmInstFunc table[] = {
[FCVT] = asm_f_2r, [FCVTZS] = asm_f_2r, [FCVTZU] = asm_f_2r,
};

void assemble_inst(Inst *inst, const ParseInfo *info, Code *code) {
void assemble_inst(Inst *inst, ParseInfo *info, Code *code) {
code->flag = 0;
code->len = 0;

Expand Down
79 changes: 54 additions & 25 deletions src/as/arch/aarch64/ir_asm_aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,44 @@
#include <assert.h>

#include "aarch64_code.h"
#include "gen_section.h"
#include "parse_asm.h"
#include "table.h"
#include "util.h"

bool calc_label_address(uintptr_t start_address, Vector **section_irs, Table *label_table) {
static void sec_add_data(SectionInfo *section, const void *data, size_t bytes) {
assert(!(section->flag & SF_BSS));
data_append(section->ds, data, bytes);
}

static void sec_add_code(SectionInfo *section, const void *buf, size_t bytes) {
sec_add_data(section, buf, bytes);
}

static void sec_add_bss(SectionInfo *section, size_t size) {
assert(section->flag & SF_BSS);
section->bss_size += size;
}

static void sec_align_size(SectionInfo *section, size_t align) {
if (align > section->align)
section->align = align;

if (section->flag & SF_BSS)
section->bss_size = ALIGN(section->bss_size, align);
else
data_align(section->ds, align);
}

bool calc_label_address(uintptr_t start_address, Table *section_infos, Table *label_table) {
bool settle = true;
uintptr_t address = start_address;
for (int sec = 0; sec < SECTION_COUNT; ++sec) {
address = align_next_section(sec, address);
section_start_addresses[sec] = address;
const Name *name;
SectionInfo *section;
for (int it = 0; (it = table_iterate(section_infos, it, &name, (void**)&section)) != -1; ) {
address = ALIGN(address, section->align);
section->start_addresses = address;

Vector *irs = section_irs[sec];
Vector *irs = section->irs;
for (int i = 0, len = irs->len; i < len; ++i) {
IR *ir = irs->data[i];
ir->address = address;
Expand All @@ -43,8 +68,8 @@ bool calc_label_address(uintptr_t start_address, Vector **section_irs, Table *la
break;
case IR_ALIGN:
ir->address = address = ALIGN(address, ir->align);
if ((size_t)ir->align > section_aligns[sec]) {
section_aligns[sec] = ir->align;
if ((size_t)ir->align > section->align) {
section->align = ir->align;
settle = false;
}
break;
Expand All @@ -66,12 +91,14 @@ bool calc_label_address(uintptr_t start_address, Vector **section_irs, Table *la
return settle;
}

bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *unresolved) {
bool resolve_relative_address(Table *section_infos, Table *label_table, Vector *unresolved) {
assert(unresolved != NULL);
vec_clear(unresolved);
bool size_upgraded = false;
for (int sec = 0; sec < SECTION_COUNT; ++sec) {
Vector *irs = section_irs[sec];
const Name *name;
SectionInfo *section;
for (int it = 0; (it = table_iterate(section_infos, it, &name, (void**)&section)) != -1; ) {
Vector *irs = section->irs;
uintptr_t start_address = irs->len > 0 ? ((IR*)irs->data[0])->address : 0;
for (int i = 0, len = irs->len; i < len; ++i) {
IR *ir = irs->data[i];
Expand All @@ -90,7 +117,7 @@ bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *
UnresolvedInfo *info = malloc_or_die(sizeof(*info));
info->kind = expr->flag == LF_PAGEOFF ? UNRES_PCREL_LO : UNRES_GOT_LO;
info->label = value.label;
info->src_section = sec;
info->src_section = section;
info->offset = address - start_address;
info->add = value.offset;
vec_push(unresolved, info);
Expand Down Expand Up @@ -120,7 +147,7 @@ bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *
UnresolvedInfo *info = malloc_or_die(sizeof(*info));
info->kind = table[i][1];
info->label = value.label;
info->src_section = sec;
info->src_section = section;
info->offset = address - start_address;
info->add = value.offset;
vec_push(unresolved, info);
Expand Down Expand Up @@ -149,7 +176,7 @@ bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *
UnresolvedInfo *info = malloc_or_die(sizeof(*info));
info->kind = table[i][1];
info->label = value.label;
info->src_section = sec;
info->src_section = section;
info->offset = address - start_address;
info->add = value.offset;
vec_push(unresolved, info);
Expand All @@ -169,7 +196,7 @@ bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *
/*UnresolvedInfo *info = malloc_or_die(sizeof(*info));
info->kind = UNRES_EXTERN;
info->label = value.label;
info->src_section = sec;
info->src_section = section;
info->offset = address - start_address;
info->add = value.offset - 4;
vec_push(unresolved, info);
Expand Down Expand Up @@ -206,7 +233,7 @@ bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *
UnresolvedInfo *info = malloc_or_die(sizeof(*info));
info->kind = UNRES_CALL;
info->label = value.label;
info->src_section = sec;
info->src_section = section;
info->offset = address - start_address;
info->add = value.offset;
vec_push(unresolved, info);
Expand All @@ -228,7 +255,7 @@ bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *
UnresolvedInfo *info = malloc_or_die(sizeof(*info));
info->kind = UNRES_ABS64; // TODO:
info->label = value.label;
info->src_section = sec;
info->src_section = section;
info->offset = address - start_address;
info->add = value.offset;
ir->expr.addend = value.offset;
Expand All @@ -247,25 +274,27 @@ bool resolve_relative_address(Vector **section_irs, Table *label_table, Vector *
return !size_upgraded;
}

void emit_irs(Vector **section_irs) {
for (int sec = 0; sec < SECTION_COUNT; ++sec) {
Vector *irs = section_irs[sec];
void emit_irs(Table *section_infos) {
const Name *name;
SectionInfo *section;
for (int it = 0; (it = table_iterate(section_infos, it, &name, (void**)&section)) != -1; ) {
Vector *irs = section->irs;
for (int i = 0, len = irs->len; i < len; ++i) {
IR *ir = irs->data[i];
switch (ir->kind) {
case IR_LABEL:
break;
case IR_CODE:
add_code(ir->code.buf, ir->code.len);
sec_add_code(section, ir->code.buf, ir->code.len);
break;
case IR_DATA:
add_section_data(sec, ir->data.buf, ir->data.len);
sec_add_data(section, ir->data.buf, ir->data.len);
break;
case IR_BSS:
add_bss(ir->bss);
sec_add_bss(section, ir->bss);
break;
case IR_ALIGN:
align_section_size(sec, ir->align);
sec_align_size(section, ir->align);
break;
case IR_EXPR_BYTE:
case IR_EXPR_SHORT:
Expand All @@ -278,7 +307,7 @@ void emit_irs(Vector **section_irs) {
int64_t value = 0;
#endif
int size = 1 << (ir->kind - IR_EXPR_BYTE);
add_section_data(sec, &value, size); // TODO: Target endian
sec_add_data(section, &value, size); // TODO: Target endian
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/as/arch/riscv64/asm_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inline bool is_im12(int64_t x) {
return x <= ((1L << 11) - 1) && x >= -(1L << 11);
}

inline bool assemble_error(const ParseInfo *info, const char *message) {
inline bool assemble_error(ParseInfo *info, const char *message) {
parse_error(info, message);
return false;
}
Expand Down Expand Up @@ -638,7 +638,7 @@ static const AsmInstFunc table[] = {
[FCVT_D_S] = asm_2fr, [FCVT_S_D] = asm_2fr,
};

void assemble_inst(Inst *inst, const ParseInfo *info, Code *code) {
void assemble_inst(Inst *inst, ParseInfo *info, Code *code) {
code->flag = 0;
code->len = 0;

Expand Down
Loading

0 comments on commit 070f154

Please sign in to comment.