Skip to content

Commit c70fe65

Browse files
authored
Improve User Linking and ELF Loading (#299)
* Add note to assembly to quiet GNU tools complaining about executable stack. * Use explicit linker script for userspace programs, to avoid unexpected behaviors from platform linking scripts. * Remove linker -Map argument used inconsistently. * Missing zero, how embarrassing! * Update linker command line and scripts to generate executables that cleanly start at 0x8000000. * Rework ELF loader to work correctly with multiple loadable segments that each may be partially initialized. * Align bss section independently, so that it is not combined with prior sections. * Separate elf definition in elf.h and process loader in loader.[ch]
1 parent 08be8c6 commit c70fe65

10 files changed

Lines changed: 297 additions & 202 deletions

File tree

Makefile.config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ KERNEL_CCFLAGS=-Wall -c -ffreestanding -fno-pie -g -std=gnu99
44
# which is likely to work on native linux-x86.
55
#
66
CC=gcc -m32
7-
LD=ld -melf_i386
8-
#LD=ld -arch i386
7+
LD=ld
8+
KERNEL_LD=ld -melf_i386
99
AR=ar
1010
OBJCOPY=objcopy
1111
ISOGEN=genisoimage
@@ -15,6 +15,7 @@ ISOGEN=genisoimage
1515
# add cross/bin to your path, and uncomment these lines:
1616
#CC=i686-elf-gcc
1717
#LD=i686-elf-ld
18+
#KERNEL_LD=i686-elf-ld
1819
#AR=i686-elf-ar
1920
#OBJCOPY=i686-elf-objcopy
2021

kernel/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include ../Makefile.config
22

3-
KERNEL_OBJECTS=kernelcore.o main.o console.o page.o keyboard.o mouse.o event_queue.o clock.o interrupt.o kmalloc.o pic.o ata.o cdromfs.o string.o bitmap.o graphics.o font.o syscall_handler.o process.o mutex.o list.o pagetable.o rtc.o kshell.o fs.o hash_set.o diskfs.o serial.o elf.o device.o kobject.o pipe.o bcache.o printf.o is_valid.o window.o
3+
KERNEL_OBJECTS=kernelcore.o main.o console.o page.o keyboard.o mouse.o event_queue.o clock.o interrupt.o kmalloc.o pic.o ata.o cdromfs.o string.o bitmap.o graphics.o font.o syscall_handler.o process.o mutex.o list.o pagetable.o rtc.o kshell.o fs.o hash_set.o diskfs.o serial.o loader.o device.o kobject.o pipe.o bcache.o printf.o is_valid.o window.o
44

55
basekernel.img: bootblock kernel
66
cat bootblock kernel /dev/zero | head -c 1474560 > basekernel.img
@@ -12,10 +12,10 @@ bootblock: bootblock.elf
1212
${OBJCOPY} -O binary $< $@
1313

1414
kernel.elf: ${KERNEL_OBJECTS}
15-
${LD} ${KERNEL_LDFLAGS} -Ttext 0x10000 ${KERNEL_OBJECTS} -o $@
15+
${KERNEL_LD} ${KERNEL_LDFLAGS} -Ttext 0x10000 ${KERNEL_OBJECTS} -o $@
1616

1717
bootblock.elf: bootblock.o
18-
${LD} ${KERNEL_LDFLAGS} -Ttext 0 $< -o $@
18+
${KERNEL_LD} ${KERNEL_LDFLAGS} -Ttext 0 $< -o $@
1919

2020
%.o: %.c
2121
${CC} ${KERNEL_CCFLAGS} -I ../include $< -o $@

kernel/elf.c

Lines changed: 0 additions & 182 deletions
This file was deleted.

kernel/elf.h

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2015-2019 The University of Notre Dame
2+
Copyright (C) 2015-2025 The University of Notre Dame
33
This software is distributed under the GNU General Public License.
44
See the file LICENSE for details.
55
*/
@@ -8,16 +8,95 @@ See the file LICENSE for details.
88
#define ELF_H
99

1010
#include "kernel/types.h"
11-
#include "process.h"
12-
#include "fs.h"
1311

14-
/*
15-
elf_load opens the given filename, and if it contains a valid
16-
ELF executable, allocates space in the current process' pagetable,
17-
loads the text, data, and bss into memory, and updates the
18-
entry point value in the current process structure.
19-
*/
12+
struct elf_header {
13+
char ident[16];
14+
uint16_t type;
15+
uint16_t machine;
16+
uint32_t version;
17+
uint32_t entry;
18+
uint32_t program_offset;
19+
uint32_t section_offset;
20+
uint32_t flags;
21+
uint16_t header_size;
22+
uint16_t phentsize;
23+
uint16_t phnum;
24+
uint16_t shentsize;
25+
uint16_t shnum;
26+
uint16_t shstrndx;
27+
};
28+
29+
#define ELF_HEADER_TYPE_NONE 0
30+
#define ELF_HEADER_TYPE_OBJECT 1
31+
#define ELF_HEADER_TYPE_EXECUTABLE 2
32+
#define ELF_HEADER_TYPE_DYNAMIC 3
33+
#define ELF_HEADER_TYPE_CORE 4
34+
35+
#define ELF_HEADER_MACHINE_I386 3
36+
#define ELF_HEADER_MACHINE_ARM 40
37+
#define ELF_HEADER_MACHINE_X86_64 62
38+
39+
#define ELF_HEADER_VERSION 1
40+
41+
struct elf_program {
42+
uint32_t type;
43+
uint32_t offset;
44+
uint32_t vaddr;
45+
uint32_t paddr;
46+
uint32_t file_size;
47+
uint32_t memory_size;
48+
uint32_t flags;
49+
uint32_t align;
50+
};
51+
52+
#define ELF_PROGRAM_TYPE_NULL 0
53+
#define ELF_PROGRAM_TYPE_LOADABLE 1
54+
#define ELF_PROGRAM_TYPE_DYNAMIC 2
55+
#define ELF_PROGRAM_TYPE_INTERPRETER 3
56+
#define ELF_PROGRAM_TYPE_NOTE 4
57+
#define ELF_PROGRAM_TYPE_SHARED_LIBRARY 5
58+
#define ELF_PROGRAM_TYPE_PROGRAM_HEADER 6
59+
#define ELF_PROGRAM_TYPE_THREAD_LOCAL 7
60+
#define ELF_PROGRAM_TYPE_GNU_EH_FRAME 0x6474e550
61+
#define ELF_PROGRAM_TYPE_GNU_STACK 0x6474e551
62+
#define ELF_PROGRAM_TYPE_GNU_RELRO 0x6474e552
63+
64+
#define ELF_PROGRAM_FLAGS_EXEC 1
65+
#define ELF_PROGRAM_FLAGS_WRITE 2
66+
#define ELF_PROGRAM_FLAGS_READ 4
67+
68+
struct elf_section {
69+
uint32_t name;
70+
uint32_t type;
71+
uint32_t flags;
72+
uint32_t address;
73+
uint32_t offset;
74+
uint32_t size;
75+
uint32_t link;
76+
uint32_t info;
77+
uint32_t alignment;
78+
uint32_t entry_size;
79+
};
80+
81+
#define ELF_SECTION_TYPE_NULL 0
82+
#define ELF_SECTION_TYPE_PROGRAM 1
83+
#define ELF_SECTION_TYPE_SYMBOL_TABLE 2
84+
#define ELF_SECTION_TYPE_STRING_TABLE 3
85+
#define ELF_SECTION_TYPE_RELA 4
86+
#define ELF_SECTION_TYPE_HASH 5
87+
#define ELF_SECTION_TYPE_DYNAMIC 6
88+
#define ELF_SECTION_TYPE_NOTE 7
89+
#define ELF_SECTION_TYPE_BSS 8
2090

21-
int elf_load(struct process *p, struct fs_dirent *d, addr_t * entry);
91+
#define ELF_SECTION_FLAGS_WRITE 1
92+
#define ELF_SECTION_FLAGS_MEMORY 2
93+
#define ELF_SECTION_FLAGS_EXEC 8
94+
#define ELF_SECTION_FLAGS_MERGE 16
95+
#define ELF_SECTION_FLAGS_STRINGS 32
96+
#define ELF_SECTION_FLAGS_INFO_LINK 64
97+
#define ELF_SECTION_FLAGS_LINK_ORDER 128
98+
#define ELF_SECTION_FLAGS_NONSTANDARD 256
99+
#define ELF_SECTION_FLAGS_GROUP 512
100+
#define ELF_SECTION_FLAGS_TLS 1024
22101

23102
#endif

0 commit comments

Comments
 (0)