summaryrefslogtreecommitdiffstats
path: root/exe.ld
blob: 810d39ac5172221fd91e1fc117130e941b81566f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
OUTPUT_ARCH(i8086)
ENTRY(___start)
SECTIONS
{
	/*
	 * The PSP is really at offset -256, but that drives the
	 * current version of ld crazy.	 Doing it this way is OK *if
	 * and only if* we don't care about 32-bit (R_386_32)
	 * relocations producing correct results.  Pure MZ EXE files
	 * don't support 32-bit relocations anyway, so this is OK for
	 * now.
	 */
	_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) : {
		/* Overridable default for __start */
		PROVIDE(___start! = .);
		PROVIDE(___start = .);

		*(_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_start = .);
		. = . + ___stack_size;
		PROVIDE(___stack_end = .);
	}

	HIDDEN(.dgroup.size.check =
		ASSERT(. - ADDR(DGROUP) <= 0x10000,
		"Error: DGROUP too large"));

	far_BSS ALIGN(16) : {
		PROVIDE(___farbss_start = .);
		*(SORT_BY_NAME(SORT_BY_ALIGNMENT(?*_BSS*)))
		PROVIDE(___farbss_end = .);
	}

	PROVIDE(___end = .);
	HIDDEN(___.end = .);

	/*
	 * Anything beyond this point is used by elf2exe and are not
	 * part of the final output. However, the linker loudly
	 * objects if the dynamic section is not part of a loadable
	 * segment, so we remove it in postprocessing.
	 */

	/* PIE linking: give us all the dynamic data including relocations */
	.dynamic  : {
		/* Special DT tags used by elf2exe */

		/* Final file size */
		LONG(0x6bb31701);
		LONG(___.filesize);

		/* Final memory size */
		LONG(0x6bb31702);
		LONG(___.end);		/* Final memory size */

		/* Segment base of entry point (used to compute CS) */
		LONG(0x6bb31703);
		LONG("___start!");

		/* Segment base of the stack (used to compute SS) */
		LONG(0x6bb31704);
		LONG("___stack_end!");

		/*
		 * Base of the stack, used to compute SP, and usable
		 * as early temp storage as it does not need to be
		 * zeroed, unlike BSS.
		 */
		LONG(0x6bb31705);
		LONG(___stack_start);

		/* Size of the stack */
		LONG(0x6bb31706);
		LONG(___stack_size);

		KEEP(*(.dynamic))

		/*
		 * Make sure the dynamic structure is terminated. This
		 * may be redundant with linker-generated data, but won't
		 * hurt; the worst case is 8 bytes wasted.
		 */
		LONG(0);
		LONG(0);
	 }
	 .dynsym   : { KEEP(*(.dynsym)) }
	 .dynstr   : { KEEP(*(.dynstr)) }
	 .gnu.hash : { KEEP(*(.gnu.hash)) }
	 .rel.dyn  : { KEEP(*(.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_*)
		  *(.hash)
		  *(.interp)
	}
}