summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2019-07-27 12:09:06 -0700
committerH. Peter Anvin <hpa@zytor.com>2019-07-27 12:13:03 -0700
commit8201939717a860259eee4db076e76833e4a1299a (patch)
tree63e0ee7cc11d15df51d957f0d14d2cf4822c8707
downloadsamples-8201939717a860259eee4db076e76833e4a1299a.tar.gz
samples-8201939717a860259eee4db076e76833e4a1299a.tar.xz
samples-8201939717a860259eee4db076e76833e4a1299a.zip
Some initial sample code
-rw-r--r--.gitignore10
-rw-r--r--Makefile21
-rw-r--r--com.ld106
-rw-r--r--exe.ld128
-rw-r--r--testme.s83
5 files changed, 348 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3a66ca3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,10 @@
+*.elf
+*.exe
+*.com
+*.bin
+*.lst
+*.map
+*.bak
+*~
+\#*
+.#*
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..dd1bd7b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,21 @@
+CROSS ?= ../i386-linux/bin/
+
+AS = $(CROSS)as
+ASFLAGS = --32
+
+LD = $(CROSS)ld
+LDFLAGS = -m elf_i386 -q -z max-page-size=16
+
+OBJS = testme.o
+ALL = exe.elf com.elf
+
+all: $(ALL)
+
+%.elf: %.ld $(OBJS)
+ $(LD) $(LDFLAGS) -Map=$*.map -T $*.ld -o $@ $(OBJS)
+
+%.s: %.o
+ $(AS) $(ASFLAGS) -aghlms=$*.lst -o $@ $<
+
+clean:
+ rm -f *.o *.elf *.exe *.com *.bin *.lst *.map
diff --git a/com.ld b/com.ld
new file mode 100644
index 0000000..82642d5
--- /dev/null
+++ b/com.ld
@@ -0,0 +1,106 @@
+OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+OUTPUT_ARCH(i386)
+ENTRY(__start)
+SECTIONS
+{
+ . = 0;
+
+ DGROUP (NOLOAD) : {
+ *(*!*)
+ PROVIDE(__psp! = .);
+ PROVIDE(___bss_start! = .);
+ PROVIDE(___bss_end! = .);
+ PROVIDE(___end! = .);
+ }
+
+ _PSP (NOLOAD) : {
+ PROVIDE(__psp = .);
+ /* May have additional symbols provided by the runtime */
+ KEEP(*(_PSP));
+ . = __psp + 256;
+ }
+
+ _TEXT : {
+ *(SORT_NONE(_START*))
+ *(SORT_BY_ALIGNMENT(_TEXT*) SORT_BY_ALIGNMENT(.text*))
+ }
+ _RODATA : {
+ *(SORT_BY_ALIGNMENT(_RODATA*) SORT_BY_ALIGNMENT(.rodata*))
+ }
+ _DATA : {
+ *(SORT_BY_ALIGNMENT(_DATA*) SORT_BY_ALIGNMENT(.data*))
+ }
+
+ PROVIDE(___filesize = .);
+ HIDDEN(___.filesize = .);
+
+ _BSS : {
+ PROVIDE(___bss_start = .);
+ *(SORT_BY_ALIGNMENT(_BSS*) SORT_BY_ALIGNMENT(.bss*)
+ SORT_BY_ALIGNMENT(COMMON))
+ PROVIDE(___bss_end = .);
+ }
+
+ PROVIDE(___stack_size = 65536 + ADDR(DGROUP) - .);
+ _STACK (NOLOAD) : {
+ PROVIDE(___stack_base = .);
+ . = . + ___stack_size;
+ PROVIDE(___stack_top = .);
+ }
+
+ PROVIDE(___end = .);
+
+ /* For PIE linking */
+ .dynamic : AT(___.filesize) {
+ PROVIDE(___DYNAMIC = .);
+ *(.dynamic)
+ }
+ .dynsym : { *(.dynsym) }
+ .dynstr : { *(.dynstr) }
+ .hash : { *(.hash) }
+ .gnu.hash : { *(.gnu.hash) }
+ .rel.dyn : { *(.rel*) }
+
+ /* Stabs debugging sections. */
+ .stab 0 : { *(.stab) }
+ .stabstr 0 : { *(.stabstr) }
+ .stab.excl 0 : { *(.stab.excl) }
+ .stab.exclstr 0 : { *(.stab.exclstr) }
+ .stab.index 0 : { *(.stab.index) }
+ .stab.indexstr 0 : { *(.stab.indexstr) }
+ .comment 0 : { *(.comment) }
+ .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) }
+ /* DWARF debug sections.
+ Symbols in the DWARF debugging sections are relative to the beginning
+ of the section so we begin them at 0. */
+ /* DWARF 1 */
+ .debug 0 : { *(.debug) }
+ .line 0 : { *(.line) }
+ /* GNU DWARF 1 extensions */
+ .debug_srcinfo 0 : { *(.debug_srcinfo) }
+ .debug_sfnames 0 : { *(.debug_sfnames) }
+ /* DWARF 1.1 and DWARF 2 */
+ .debug_aranges 0 : { *(.debug_aranges) }
+ .debug_pubnames 0 : { *(.debug_pubnames) }
+ /* DWARF 2 */
+ .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
+ .debug_abbrev 0 : { *(.debug_abbrev) }
+ .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) }
+ .debug_frame 0 : { *(.debug_frame) }
+ .debug_str 0 : { *(.debug_str) }
+ .debug_loc 0 : { *(.debug_loc) }
+ .debug_macinfo 0 : { *(.debug_macinfo) }
+ /* SGI/MIPS DWARF 2 extensions */
+ .debug_weaknames 0 : { *(.debug_weaknames) }
+ .debug_funcnames 0 : { *(.debug_funcnames) }
+ .debug_typenames 0 : { *(.debug_typenames) }
+ .debug_varnames 0 : { *(.debug_varnames) }
+ /* DWARF 3 */
+ .debug_pubtypes 0 : { *(.debug_pubtypes) }
+ .debug_ranges 0 : { *(.debug_ranges) }
+ /* DWARF Extension. */
+ .debug_macro 0 : { *(.debug_macro) }
+ .debug_addr 0 : { *(.debug_addr) }
+ .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }
+ /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }
+}
diff --git a/exe.ld b/exe.ld
new file mode 100644
index 0000000..e0c2f37
--- /dev/null
+++ b/exe.ld
@@ -0,0 +1,128 @@
+OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+OUTPUT_ARCH(i386)
+ENTRY(__start)
+SECTIONS
+{
+ _PSP 0xfff00 (NOLOAD) : {
+ PROVIDE(__psp! = .);
+ PROVIDE(__psp = .);
+ /* May have additional symbols provided by the runtime */
+ KEEP(*(_PSP));
+ . = __psp + 256;
+ }
+
+ . = 0;
+
+ far_TEXT : {
+ *(SORT_BY_NAME(SORT_BY_ALIGNMENT(?*_TEXT*)))
+ }
+ far_DATA : {
+ *(SORT_BY_NAME(SORT_BY_ALIGNMENT(?*_*DATA*)))
+ }
+
+ _TEXT ALIGN(16) : {
+ *(_START*!* _TEXT*!*)
+ *(SORT_NONE(_START*))
+ *(SORT_BY_ALIGNMENT(_TEXT*) SORT_BY_ALIGNMENT(.text*))
+ }
+
+ DGROUP : {
+ *(!DGROUP _*DATA*!* _BSS*!* _STACK*!*)
+ PROVIDE(___data_start! = .);
+ PROVIDE(___bss_start! = .);
+ PROVIDE(___bss_end! = .);
+ PROVIDE(___stack_start! = .);
+ PROVIDE(___stack_end! = .);
+ }
+
+ PROVIDE(___data_start = .);
+
+ _RODATA : {
+ *(SORT_BY_ALIGNMENT(_RODATA*) SORT_BY_ALIGNMENT(.rodata*))
+ *(SORT_BY_ALIGNMENT(.rodata*))
+ }
+ _DATA : {
+ *(SORT_BY_ALIGNMENT(_DATA*) SORT_BY_ALIGNMENT(.data*))
+ }
+
+ PROVIDE(___filesize = .);
+ HIDDEN(___.filesize = .);
+
+ _BSS : {
+ PROVIDE(___bss_start = .);
+ *(SORT_BY_ALIGNMENT(_BSS*) SORT_BY_ALIGNMENT(.bss)
+ SORT_BY_ALIGNMENT(COMMON))
+ PROVIDE(___bss_end = .);
+ }
+
+ . = ALIGN(16);
+ /* Default near stack/heap segment size, can be overridden */
+ PROVIDE(___stack_size = 65536 + ADDR(DGROUP) - .);
+ _STACK (NOLOAD) : {
+ PROVIDE(___stack_base = .);
+ . = . + ___stack_size;
+ PROVIDE(___stack_top = .);
+ }
+
+ far_BSS ALIGN(16) : {
+ PROVIDE(___farbss_start = .);
+ *(SORT_BY_NAME(SORT_BY_ALIGNMENT(?*_BSS*)))
+ PROVIDE(___farbss_end = .);
+ }
+
+ PROVIDE(___end = .);
+
+ /* For PIE linking */
+ .dynamic : AT(___.filesize) {
+ PROVIDE(___DYNAMIC = .);
+ *(.dynamic)
+ }
+ .dynsym : { *(.dynsym) }
+ .dynstr : { *(.dynstr) }
+ .hash : { *(.hash) }
+ .gnu.hash : { *(.gnu.hash) }
+ .rel.dyn : { *(.rel*) }
+
+ /* Stabs debugging sections. */
+ .stab 0 : { *(.stab) }
+ .stabstr 0 : { *(.stabstr) }
+ .stab.excl 0 : { *(.stab.excl) }
+ .stab.exclstr 0 : { *(.stab.exclstr) }
+ .stab.index 0 : { *(.stab.index) }
+ .stab.indexstr 0 : { *(.stab.indexstr) }
+ .comment 0 : { *(.comment) }
+ .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) }
+ /* DWARF debug sections.
+ Symbols in the DWARF debugging sections are relative to the beginning
+ of the section so we begin them at 0. */
+ /* DWARF 1 */
+ .debug 0 : { *(.debug) }
+ .line 0 : { *(.line) }
+ /* GNU DWARF 1 extensions */
+ .debug_srcinfo 0 : { *(.debug_srcinfo) }
+ .debug_sfnames 0 : { *(.debug_sfnames) }
+ /* DWARF 1.1 and DWARF 2 */
+ .debug_aranges 0 : { *(.debug_aranges) }
+ .debug_pubnames 0 : { *(.debug_pubnames) }
+ /* DWARF 2 */
+ .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
+ .debug_abbrev 0 : { *(.debug_abbrev) }
+ .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) }
+ .debug_frame 0 : { *(.debug_frame) }
+ .debug_str 0 : { *(.debug_str) }
+ .debug_loc 0 : { *(.debug_loc) }
+ .debug_macinfo 0 : { *(.debug_macinfo) }
+ /* SGI/MIPS DWARF 2 extensions */
+ .debug_weaknames 0 : { *(.debug_weaknames) }
+ .debug_funcnames 0 : { *(.debug_funcnames) }
+ .debug_typenames 0 : { *(.debug_typenames) }
+ .debug_varnames 0 : { *(.debug_varnames) }
+ /* DWARF 3 */
+ .debug_pubtypes 0 : { *(.debug_pubtypes) }
+ .debug_ranges 0 : { *(.debug_ranges) }
+ /* DWARF Extension. */
+ .debug_macro 0 : { *(.debug_macro) }
+ .debug_addr 0 : { *(.debug_addr) }
+ .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }
+ /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }
+}
diff --git a/testme.s b/testme.s
new file mode 100644
index 0000000..fbe65b1
--- /dev/null
+++ b/testme.s
@@ -0,0 +1,83 @@
+ .code16
+
+ .section "!DGROUP","a"
+ .balign 16
+.SEG.DGROUP:
+.SEG._RODATA:
+.SEG._DATA:
+.SEG._BSS:
+
+ .section "_TEXT!","a"
+ .balign 16
+.SEG._TEXT: /* Local symbol! */
+
+ .section "_TEXT$","ax"
+.L_SEG = .SEG._TEXT
+
+ .globl __start, "__start!"
+ .equ "__start!", .L.SEG
+__start:
+ mov $0,%ax
+ .reloc .-2, R_386_SEG16, .SEG.DGROUP
+ mov %ax,%ds
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+ nop
+
+ /*
+ * Return the value of _foo in ax, and its linear address in edx
+ */
+ .globl _get_foo, "_get_foo!"
+ .equ "_get_foo!", .L.SEG
+_get_foo:
+ mov _foo,%ax /* foo is in our own data segment */
+ .reloc .-2, R_386_SUB16, .L.SEG
+ mov $_foo,%edx
+ ret
+
+ /*
+ * Return a pointer to the command line (from the PSP) in
+ * es:bx
+ */
+ .globl _get_cmdline, "_get_cmdline!"
+ .equ "_get_cmdline!", .L.SEG
+_get_cmdline:
+ mov $0,%ax
+ .reloc .-2, R_386_SEG16, "__psp!"
+ mov %ax,%es
+ mov $__psp + 127,%dx
+ .reloc .-2, R_386_SUB16, "__psp!"
+ ret
+
+ .section "_DATA$","aw"
+.L.SEG = .SEG._DATA
+ .equ "_other!", .L.SEG
+_other: .word 0x5678
+ .equ "_foo!", .L.SEG
+_foo: .word 0x1234
+
+ .section "_BSS$","aw",@nobits
+.L.SEG = .SEG._BSS
+ .balign 2
+ .globl _nasa, "_nasa!"
+ .equ "_nasa!", .L.SEG
+_nasa: .space 512