align segments
This commit is contained in:
parent
0c8ad22d45
commit
b57dc4a492
5 changed files with 33 additions and 7 deletions
BIN
masm/a.out
Executable file
BIN
masm/a.out
Executable file
Binary file not shown.
34
masm/asm.c
34
masm/asm.c
|
@ -17,6 +17,8 @@ extern char *current_file;
|
||||||
#define SYMSEC_STUB -1
|
#define SYMSEC_STUB -1
|
||||||
#define SYMSEC_EXTERN -1
|
#define SYMSEC_EXTERN -1
|
||||||
|
|
||||||
|
#define SEC_ALIGN 0x1000
|
||||||
|
|
||||||
static int create_symbol(struct assembler *assembler,
|
static int create_symbol(struct assembler *assembler,
|
||||||
const char name[MAX_LEX_LENGTH],
|
const char name[MAX_LEX_LENGTH],
|
||||||
ssize_t section_idx,
|
ssize_t section_idx,
|
||||||
|
@ -338,7 +340,7 @@ static int assemble_phdr(struct assembler *assembler, Elf32_Phdr **res,
|
||||||
hdr->p_paddr = 0;
|
hdr->p_paddr = 0;
|
||||||
hdr->p_filesz = htonl(size);
|
hdr->p_filesz = htonl(size);
|
||||||
hdr->p_memsz = htonl(size);
|
hdr->p_memsz = htonl(size);
|
||||||
hdr->p_align = htonl(0x1000);
|
hdr->p_align = htonl(SEC_ALIGN);
|
||||||
}
|
}
|
||||||
|
|
||||||
*res = phdr;
|
*res = phdr;
|
||||||
|
@ -530,17 +532,30 @@ static void update_offsets(struct assembler *assembler, Elf32_Ehdr *ehdr)
|
||||||
ptr += len * sizeof(Elf32_Rela);
|
ptr += len * sizeof(Elf32_Rela);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// section padding
|
||||||
|
{
|
||||||
|
uint32_t mod = ptr % SEC_ALIGN;
|
||||||
|
if (mod != 0)
|
||||||
|
assembler->secalign = (SEC_ALIGN - mod);
|
||||||
|
else
|
||||||
|
assembler->secalign = 0;
|
||||||
|
ptr += assembler->secalign;
|
||||||
|
}
|
||||||
|
|
||||||
// sections
|
// sections
|
||||||
|
size_t v_addr = 0x00400000;
|
||||||
for (uint32_t i = 0; i < assembler->sectab.len; i++) {
|
for (uint32_t i = 0; i < assembler->sectab.len; i++) {
|
||||||
struct section *sec = &assembler->sectab.sections[i];
|
struct section *sec = &assembler->sectab.sections[i];
|
||||||
int idx = sec->shdr_idx;
|
uint32_t idx = sec->shdr_idx;
|
||||||
|
uint32_t size = ntohl(phdr[i].p_filesz);
|
||||||
phdr[i].p_offset = htonl(ptr);
|
phdr[i].p_offset = htonl(ptr);
|
||||||
phdr[i].p_vaddr = htonl(ptr);
|
phdr[i].p_vaddr = htonl(v_addr);
|
||||||
phdr[i].p_paddr = htonl(ptr);
|
phdr[i].p_paddr = htonl(v_addr);
|
||||||
shdr[idx].sh_offset = htonl(ptr);
|
shdr[idx].sh_offset = htonl(ptr);
|
||||||
shdr[idx].sh_size = phdr[i].p_filesz;
|
shdr[idx].sh_size = phdr[i].p_filesz;
|
||||||
shdr[idx].sh_addr = phdr[i].p_vaddr;
|
shdr[idx].sh_addr = phdr[i].p_vaddr;
|
||||||
ptr += ntohl(phdr[i].p_filesz);
|
v_addr += size;
|
||||||
|
ptr += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
// symtab
|
// symtab
|
||||||
|
@ -567,8 +582,7 @@ static void update_offsets(struct assembler *assembler, Elf32_Ehdr *ehdr)
|
||||||
|
|
||||||
static void update_sym_shindx(struct assembler *assembler)
|
static void update_sym_shindx(struct assembler *assembler)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < assembler->symtab.len; i++)
|
for (size_t i = 0; i < assembler->symtab.len; i++) {
|
||||||
{
|
|
||||||
Elf32_Sym *sym = &assembler->symtab.symbols[i];
|
Elf32_Sym *sym = &assembler->symtab.symbols[i];
|
||||||
ssize_t sec = assembler->symtab.sections[i];
|
ssize_t sec = assembler->symtab.sections[i];
|
||||||
|
|
||||||
|
@ -605,6 +619,12 @@ static int write_file(struct assembler *assembler, Elf32_Ehdr *ehdr,
|
||||||
fwrite(ptr, sizeof(Elf32_Rela), len, out);
|
fwrite(ptr, sizeof(Elf32_Rela), len, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// section padding
|
||||||
|
for (uint32_t i = 0; i < assembler->secalign; i++) {
|
||||||
|
uint8_t zero = 0;
|
||||||
|
fwrite(&zero, 1, 1, out);
|
||||||
|
}
|
||||||
|
|
||||||
// sections
|
// sections
|
||||||
for (uint32_t i = 0; i < assembler->sectab.len; i++) {
|
for (uint32_t i = 0; i < assembler->sectab.len; i++) {
|
||||||
struct section *sec = &assembler->sectab.sections[i];
|
struct section *sec = &assembler->sectab.sections[i];
|
||||||
|
|
|
@ -213,6 +213,7 @@ struct assembler {
|
||||||
|
|
||||||
/// Segments
|
/// Segments
|
||||||
struct section_table sectab;
|
struct section_table sectab;
|
||||||
|
uint32_t secalign; // align sections to 0x1000 when writing
|
||||||
|
|
||||||
/// program header
|
/// program header
|
||||||
Elf32_Phdr *phdr;
|
Elf32_Phdr *phdr;
|
||||||
|
|
BIN
masm/out.o
Normal file
BIN
masm/out.o
Normal file
Binary file not shown.
|
@ -1,3 +1,8 @@
|
||||||
|
.rodata
|
||||||
|
gay:
|
||||||
|
.word 3
|
||||||
|
|
||||||
|
|
||||||
.data
|
.data
|
||||||
|
|
||||||
str:
|
str:
|
||||||
|
|
Loading…
Reference in a new issue