use _start, but don't call it ;)

This commit is contained in:
PoroCYon 2019-01-26 03:56:08 +01:00 committed by PoroCYon
parent 56357dc04b
commit f6b9a927a6
5 changed files with 23 additions and 9 deletions

View File

@ -38,7 +38,7 @@ $(OBJDIR)/symbols.%.s: $(OBJDIR)/%.o.syms
$(LDRDIR)/mksyms $(LIBS) $$(cat $^) > $@
$(OBJDIR)/header.%.o: $(OBJDIR)/symbols.%.s $(LDRDIR)/header.s $(LDRDIR)/loader.s
nasm $(ASFLAGS) $< -o $@
nasm -DUSE_INTERP $(ASFLAGS) $< -o $@
$(BINDIR)/%: $(OBJDIR)/%.o $(OBJDIR)/header.%.o
$(LD) -m elf_i386 $(LDFLAGS) $^ -o $@

View File

@ -20,7 +20,7 @@ header:
; e_version
dd 1
; e_entry
dd _start
dd _smol_start
; e_phoff
dd (.segments - header)
; e_shoff

View File

@ -6,7 +6,9 @@ SECTIONS {
.header : { *(.header) }
.text : {
*(.text .text.* .rdata .rdata.* .rodata .rodata.*)
*(.text._smol_start)
*(.text._start)
*(.text .rdata .rdata.* .rodata .rodata.*)
}
.data : {

View File

@ -1,5 +1,4 @@
; vim: set ft=nasm ts=8:
[section .text]
%define LM_NAME_OFFSET 0x4
%define LM_NEXT_OFFSET 0xC
@ -22,6 +21,8 @@
lm_off_extra:
dd 0
[section .text._smol_start]
strcmp: ; (const char *s1 (esi), const char *s2 (edi))
push esi
push edi
@ -130,15 +131,15 @@ link: ; (struct link_map *root, char *symtable)
.done: ret
extern main
_start:
extern _start
_smol_start:
; try to get the 'version-agnostic' pffset of the stuff we're
; interested in
mov ebx, eax
mov esi, eax
.looper:
lodsd
cmp dword eax, _start
cmp dword eax, _smol_start
jne short .looper
sub esi, ebx
sub esi, LM_ENTRY_OFFSET_BASE+4 ; +4: take inc-after from lodsb into acct
@ -150,6 +151,6 @@ _start:
push eax
call link
call main
int3
;jmp short _start
; by abusing the linker script, _start ends up right here :)

11
src/test.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdlib.h>
#include <stdio.h>
const char *f = "foo";
__attribute__((__externally_visible__, __section__(".text._start"), __noreturn__))
int _start(void) {
printf("hello world %s\n", f);
exit(42);
__builtin_unreachable();
}