Skip to content

Commit ccf1544

Browse files
author
Noam Preil
committed
Emit KEXC relocation table with -fauto-relocation
1 parent 7e4aad8 commit ccf1544

File tree

1 file changed

+13
-40
lines changed

1 file changed

+13
-40
lines changed

linker/linker.c

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -98,40 +98,15 @@ void resolve_immediate_values(list_t *symbols, area_t *area, list_t *errors) {
9898
}
9999

100100
/* z80 only (and possibly ez80) */
101-
void auto_relocate_area(area_t *area, area_t *runtime) {
101+
void auto_relocate_area(area_t *area, area_t *relocation_table) {
102102
scas_log(L_DEBUG, "Performing automatic relocation for %s", area->name);
103-
uint8_t rst0x8 = 0xCF;
104103
int i;
105104
for (i = 0; i < area->late_immediates->length; ++i) {
106105
late_immediate_t *imm = area->late_immediates->items[i];
107-
if (imm->type != IMM_TYPE_RELATIVE) {
108-
if (imm->base_address != imm->address) {
109-
/* Relocate this */
110-
scas_log(L_DEBUG, "Adding relocation instruction for immediate at 0x%08X (inserting at 0x%08X)", imm->address, imm->instruction_address);
111-
insert_in_area(area, &rst0x8, sizeof(uint8_t), imm->instruction_address);
112-
++imm->address;
113-
/* Move everything that comes after */
114-
int k;
115-
for (k = 0; k < area->symbols->length; ++k) {
116-
symbol_t *sym = area->symbols->items[k];
117-
if (sym->type == SYMBOL_LABEL && sym->value > imm->instruction_address) {
118-
++sym->value;
119-
}
120-
}
121-
int j;
122-
for (j = 0; j < area->late_immediates->length; ++j) {
123-
late_immediate_t *_imm = area->late_immediates->items[j];
124-
if (_imm->base_address > imm->base_address) {
125-
++_imm->base_address;
126-
++_imm->instruction_address;
127-
++_imm->address;
128-
}
129-
}
130-
} else {
131-
/* Relocate this */
132-
uint16_t value = (uint16_t)(imm->address + area->final_address);
133-
append_to_area(runtime, (uint8_t*)&value, sizeof(uint16_t));
134-
}
106+
if (imm->type != IMM_TYPE_RELATIVE && imm->base_address != imm->address) {
107+
/* Relocate this */
108+
uint16_t value = (uint16_t)(imm->address + area->final_address);
109+
append_to_area(relocation_table, (uint8_t*)&value, sizeof(uint16_t));
135110
}
136111
}
137112
}
@@ -161,27 +136,25 @@ void link_objects(FILE *output, list_t *objects, linker_settings_t *settings) {
161136
list_t *symbols = create_list(); // TODO: Use a hash table
162137

163138
/* Create a new area for relocatable references */
164-
area_t *runtime;
139+
area_t *relocation_table;
165140
if (settings->automatic_relocation) {
166-
const char *sym_name = "__scas_relocatable_data";
167-
runtime = create_area("__scas_relocatable");
141+
relocation_table = create_area("__scas_relocation_table");
168142
symbol_t *sym = malloc(sizeof(symbol_t));
169143
sym->type = SYMBOL_LABEL;
170-
sym->name = malloc(strlen(sym_name) + 1);
171-
strcpy(sym->name, sym_name);
144+
sym->name = strdup("__scas_relocation_table");
172145
sym->value = 0;
173146
sym->defined_address = 0;
174147
sym->exported = 0;
175-
list_add(runtime->symbols, sym);
148+
list_add(relocation_table->symbols, sym);
176149
object_t *o = create_object();
177-
list_add(o->areas, runtime);
150+
list_add(o->areas, relocation_table);
178151
list_add(objects, o);
179152
}
180153

181154
object_t *merged = merge_objects(objects);
182155
area_t *final = create_area("FINAL");
183156

184-
runtime = get_area_by_name(merged, "__scas_relocatable");
157+
relocation_table = get_area_by_name(merged, "__scas_relocation_table");
185158

186159
scas_log(L_INFO, "Assigning final address for all areas");
187160
if (scas_runtime.options.remove_unused_functions) {
@@ -193,11 +166,11 @@ void link_objects(FILE *output, list_t *objects, linker_settings_t *settings) {
193166
area_t *area = merged->areas->items[i];
194167
relocate_area(area, address, false);
195168
if (settings->automatic_relocation) {
196-
if (area == runtime) {
169+
if (area == relocation_table) {
197170
uint16_t null_ptr = 0;
198171
append_to_area(area, (uint8_t *)&null_ptr, sizeof(uint16_t));
199172
} else {
200-
auto_relocate_area(area, runtime);
173+
auto_relocate_area(area, relocation_table);
201174
}
202175
}
203176
address += area->data_length;

0 commit comments

Comments
 (0)