aboutsummaryrefslogtreecommitdiff
path: root/ld/ldint.texinfo
blob: 81e422a010aaa2194a76b383e087ddcaa1cab965 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
\input texinfo
@setfilename ldint.info

@ifinfo
@format
START-INFO-DIR-ENTRY
* Ld-Internals: (ldint).	The GNU linker internals.
END-INFO-DIR-ENTRY
@end format
@end ifinfo

@ifinfo
This file documents the internals of the GNU linker ld.

Copyright (C) 1992 Free Software Foundation, Inc.
Contributed by Cygnus Support.

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

@ignore
Permission is granted to process this file through Tex and print the
results, provided the printed document carries copying permission
notice identical to this one except for the removal of this paragraph
(this paragraph not being relevant to the printed manual).

@end ignore
Permission is granted to copy or distribute modified versions of this
manual under the terms of the GPL (for which purpose this text may be
regarded as a program in the language TeX).
@end ifinfo

@iftex
@finalout
@setchapternewpage off
@settitle GNU Linker Internals
@titlepage
@title{A guide to the internals of the GNU linker}
@author Per Bothner, Steve Chamberlain
@author Cygnus Support
@page
@end iftex
@tex
\def\$#1${{#1}}  % Kluge: collect RCS revision info without $...$
\xdef\manvers{\$Revision$}  % For use in headers, footers too
{\parskip=0pt
\hfill Cygnus Support\par
\hfill \manvers\par
\hfill \TeX{}info \texinfoversion\par
}
@end tex

@vskip 0pt plus 1filll
Copyright @copyright{} 1992 Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

@end titlepage

@node Top, README, (dir), (dir)

This file documents the internals of the GNU linker @code{ld}.  It is a
collection of miscellaneous information with little form at this point.
Mostly, it is a repository into which you can put information about
GNU @code{ld} as you discover it (or as you design changes to @code{ld}).

@menu
* README::			The README File
* Emulations::			How linker emulations are generated
* Porting::			Porting the linker
@end menu

@node README, Emulations, Top, Top
@chapter The @file{README} File

Check the @file{README} file; it often has useful information that does not
appear anywhere else in the directory.

@node Emulations, Porting, README, Top
@chapter How linker emulations are generated

The linker is controlled by linker scripts written in a linker
control language.  A linker emulation gives the personality of
the linker, and is mainly defined by certain linker scripts.
If you want to understand how these scripts are generated,
the main file to look at is the @file{genscripts.sh} shell script,
which is invoked by the @file{Makefile} for each ``emulation''
to generate a set of 5 linker scripts.

For example, for the sun3 emulation used by ld68k, @file{genscripts.sh}
sources the file @file{emulparams/sun3.sh}, which sets the emulation
parameters, and specifies that the format is a.out, and to use
@file{scripttempl/aout.sc} to generate the linker scripts.

@code{genscripts.sh} generates 5 different linker scripts, one for each
of the @code{ld} options @samp{-z} (default), @samp{-n}, @samp{-N},
@samp{-r} and @samp{-Ur}, where each script is slightly different and is
generated using the template in @file{scripttempl/aout.sc} (for the sun3).

@node Porting, , Emulations, Top
@chapter Porting the linker

Before porting @code{ld} itself, you will need to port the BFD library;
see @file{../bfd/PORTING}.

The @dfn{host} is the system a tool runs @emph{on}.
The @dfn{target} is the system a tool runs @emph{for}; i.e.,
a tool can read and write the binaries of the target.
Most often, host==target, but @code{ld} supports cross-linking
(and to some extent the same @code{ld} binary can be used a linker
for multiple target architectures).

@menu
* New host:: Porting to a new host
* New target:: Porting to a new target
* New emulation:: Porting to a new emulation target
* Emulation script:: Writing @var{emulation}.sh
* Linker scripts:: Writing a new @var{script}.sc
* -n and -N options:: Handling -n and -N style binaries in your linker script
@end menu

@node New host, New target, , Porting
@section Porting to a new host

Pick a name for your host. Call that @var{host-type}.
You need to create the file @file{config/@var{host-type}.mh}.

@node New target, New emulation, New host, Porting
@section Porting to a new target

Pick a name for your target. Call that @var{target}.
You need to create at least @file{config/@var{target}.mt}.
It should contain

@example
EMUL=@var{emulation}
@end example

An @dfn{emulation} controls the ``personality'' of @code{ld},
such as the default linker script.  Usually, the
@var{emulation} will have the same name as the @var{target},
and you will need to create a new @var{emulation} (see below).

You also need to edit @file{Makefile.in} and possibly @file{configure.in}.
To see how to do that, search for existing examples (e.g., @code{sun3},
@code{sun4}, @code{hp300bsd}).

@node New emulation, Emulation script, New target, Porting
@section Porting to a new emulation target

Pick a name for your target. Call that @var{emulation}.
Usually, @var{emulation} and @var{target} are the same.
You need to create at least @file{emulparams/@var{emulation}.sh}.
You also need to edit @file{Makefile.in}.
To see how to do that, search for existing examples.

The file @file{emulparams/@var{emulation}.sh} defines a set of
parameters that are used to generate the emulation.  Its syntax is that
of a Bourne shell script.  It is ``sourced'' by @file{genscripts.sh}.

@node Emulation script, Linker scripts, New emulation, Porting
@section Writing @file{@var{emulation}.sh}

Usually, @file{@var{emulation}.sh} contains:
@example
EMULATION_NAME=@var{emulation}
SCRIPT_NAME=@var{script}
OUTPUT_FORMAT="@var{target-name}"
TEXT_START_ADDR=@var{text-start-addr}
PAGE_SIZE=@var{page-size}
SEGMENT_SIZE=@var{segment-size}  # If different from PAGE_SIZE.
ARCH=@var{arch}
@end example

Here:
@table @code
@item @var{target-name}
Matches the @code{filename} field of the @code{bfd_target} you want
to use.  (This is a string, and currently the first field.)
For an a.out target, @var{target-name} matches the @code{TARGETNAME}
defined in @file{../bfd/@var{target}.c}.

@item @var{arch}
The architecture: e.g., @code{m68k}, @code{sparc}, @dots{}.

@item @var{script}
The file @file{scripttempl/@var{script}.sc} is a shell script which,
when evaluated (by @file{genscripts.sh}), writes a linker script file to
standard output.  You may need to write a new script.  If you use the
a.out format or something similar, you can probably set
@example
SCRIPT_NAME=aout
@end example

@item @var{text-start-addr}
@itemx @var{page-size}
@itemx @var{segment-size}
These set the shell variables @code{TEXT_START_ADDR}, @code{PAGE_SIZE},
and @code{SEGMENT_SIZE} for use by @file{scripttempl/@var{script}.sc}.
If your script doesn't use these variables, you
don't have to define the variables,
For emulations using a.out files, you can get these
values from @file{../bfd/@var{target}.c}.
@end table

In some cases, you may need more more definitions.
For example, if you can't use @file{emultempl/generic.em},
you may need to add:
@example
TEMPLATE_NAME=@var{emulation}
@end example
and write your own @file{emultempl/@var{emulation}.em} file.

@node Linker scripts, -n and -N options, Emulation script, Porting
@section Writing a new linker script @file{scripttempl/@var{script}.sc}

You may need to write a new script file for your emulation.

Your script can use the shell variable @code{LD_FLAG}, which has the value:
@table @code
@item LD_FLAG=
when building a script to be used by default
@item LD_FLAG=n
when building a script to be used for @samp{ld -n}
@item LD_FLAG=N
when building a script to be used for @samp{ld -N}
@item LD_FLAG=r
when building a script to be used for @samp{ld -r}
@item LD_FLAG=u
when building a script to be used for @samp{ld -Ur}
@end table

The variable @code{RELOCATING} is only set if relocation is happening
(i.e., unless the linker is invoked with @samp{-r}).
Thus your script should has an action @code{@var{ACTION}}
that should only be done when relocating,
express that as:
@example
$@{RELOCATING+ ACTION@}
@end example
This is the case for most assignments, which should look like:
@example
$@{RELOCATING+ _end = .@}
@end example

Also, you should assign absolute addresses to sections only
when relocating, so:
@example
.text $@{RELOCATING+ $@{TEXT_START_ADDR@}@}:
@end example

The form:
@example
	 .section @{ ... @} > section
@end example
should be:
@example
	 .section @{ ... @} > $@{RELOCATING+ section@}
@end example

@code{RELOCATING} is set except when @code{LD_FLAG=r} or @code{LD_FLAG=u}.
@code{CONSTRUCTING} is set except when @code{LD_FLAG=u}.

Alignment of the data segments is controlled by the variables
@code{DATA_ALIGNMENT_} (note trailing underscore),
@code{DATA_ALIGNMENT_n}, @code{DATA_ALIGNMENT_N},
@code{DATA_ALIGNMENT_r}, or @code{DATA_ALIGNMENT_u} depending on the
value of @code{LD_FLAGS}.  Normally, the default value works (this is
@code{"ALIGN($@{SEGMENT_SIZE@})"} for the @samp{_n}, and @samp{__}
(default) variants; @code{"."} for the @samp{_N}, variant; and @code{""}
for the @samp{_r} and @samp{_u} variants).

@node -n and -N options, , Linker scripts, Porting
@section Handling @samp{-n} and @samp{-N} style binaries in your linker script

The @samp{-n} linker option requests the linker to create a binary
with a write-protected text segment, but not demand-pagable (@code{NMAGIC}).
SunOS starts the text segment for demand-paged binaries at 0x2020
and other binaries at 0x2000, since the exec header (0x20 bytes)
is paged in with the text.  Some other Unix variants do the same.

In that case, the @file{emulparams/@var{emulation}.sh} should define:
@table @code
@item NONPAGED_TEXT_START_ADDR
The text start address to use when linking with @samp{-n} or @samp{-N} options.
@end table

For example, on a sun4:
@example
TEXT_START_ADDR=0x2020
NONPAGED_TEXT_START_ADDR=0x2000
@end example

The @samp{-N} linker option creates a binary with a non-write-protected
text segment (@code{NMAGIC}).  This is like @samp{-n}, except that the
data segment needs not be page-aligned.

@contents
@bye