Commit | Line | Data |
---|---|---|
99ad8390 NC |
1 | # This shell script emits a C file. -*- C -*- |
2 | # It does some substitutions. | |
99ad8390 NC |
3 | if [ -z "$MACHINE" ]; then |
4 | OUTPUT_ARCH=${ARCH} | |
5 | else | |
6 | OUTPUT_ARCH=${ARCH}:${MACHINE} | |
7 | fi | |
a07ee50e CV |
8 | |
9 | case ${target} in | |
10 | *-*-cygwin*) | |
11 | move_default_addr_high=1 | |
12 | ;; | |
13 | *) | |
14 | move_default_addr_high=0; | |
15 | ;; | |
16 | esac | |
17 | ||
99ad8390 NC |
18 | rm -f e${EMULATION_NAME}.c |
19 | (echo;echo;echo;echo;echo)>e${EMULATION_NAME}.c # there, now line numbers match ;-) | |
92b93329 | 20 | fragment <<EOF |
b3adc24a | 21 | /* Copyright (C) 2006-2020 Free Software Foundation, Inc. |
f96b4a7b NC |
22 | Written by Kai Tietz, OneVision Software GmbH&CoKg. |
23 | ||
24 | This file is part of the GNU Binutils. | |
99ad8390 NC |
25 | |
26 | This program is free software; you can redistribute it and/or modify | |
27 | it under the terms of the GNU General Public License as published by | |
f96b4a7b | 28 | the Free Software Foundation; either version 3 of the License, or |
99ad8390 NC |
29 | (at your option) any later version. |
30 | ||
31 | This program is distributed in the hope that it will be useful, | |
32 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
33 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
34 | GNU General Public License for more details. | |
35 | ||
36 | You should have received a copy of the GNU General Public License | |
37 | along with this program; if not, write to the Free Software | |
f96b4a7b NC |
38 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
39 | MA 02110-1301, USA. */ | |
40 | ||
99ad8390 NC |
41 | |
42 | /* For WINDOWS_XP64 and higher */ | |
43 | /* Based on pe.em, but modified for 64 bit support. */ | |
44 | ||
45 | #define TARGET_IS_${EMULATION_NAME} | |
46 | ||
47 | #define COFF_IMAGE_WITH_PE | |
48 | #define COFF_WITH_PE | |
49 | #define COFF_WITH_pex64 | |
50 | ||
99ad8390 | 51 | #include "sysdep.h" |
3db64b00 | 52 | #include "bfd.h" |
99ad8390 | 53 | #include "bfdlink.h" |
1ff6de03 | 54 | #include "ctf-api.h" |
99ad8390 NC |
55 | #include "getopt.h" |
56 | #include "libiberty.h" | |
42627821 | 57 | #include "filenames.h" |
99ad8390 NC |
58 | #include "ld.h" |
59 | #include "ldmain.h" | |
60 | #include "ldexp.h" | |
61 | #include "ldlang.h" | |
62 | #include "ldfile.h" | |
63 | #include "ldemul.h" | |
64 | #include <ldgram.h> | |
65 | #include "ldlex.h" | |
66 | #include "ldmisc.h" | |
67 | #include "ldctor.h" | |
61e2488c | 68 | #include "ldbuildid.h" |
99ad8390 NC |
69 | #include "coff/internal.h" |
70 | ||
71 | /* FIXME: See bfd/peXXigen.c for why we include an architecture specific | |
72 | header in generic PE code. */ | |
73 | #include "coff/x86_64.h" | |
74 | #include "coff/pe.h" | |
75 | ||
61e2488c | 76 | /* FIXME: These are BFD internal header files, and we should not be |
99ad8390 NC |
77 | using it here. */ |
78 | #include "../bfd/libcoff.h" | |
61e2488c | 79 | #include "../bfd/libpei.h" |
99ad8390 NC |
80 | |
81 | #undef AOUTSZ | |
82 | #define AOUTSZ PEPAOUTSZ | |
83 | #define PEAOUTHDR PEPAOUTHDR | |
84 | ||
85 | #include "deffile.h" | |
86 | #include "pep-dll.h" | |
87 | #include "safe-ctype.h" | |
88 | ||
89 | /* Permit the emulation parameters to override the default section | |
90 | alignment by setting OVERRIDE_SECTION_ALIGNMENT. FIXME: This makes | |
91 | it seem that include/coff/internal.h should not define | |
92 | PE_DEF_SECTION_ALIGNMENT. */ | |
93 | #if PE_DEF_SECTION_ALIGNMENT != ${OVERRIDE_SECTION_ALIGNMENT:-PE_DEF_SECTION_ALIGNMENT} | |
94 | #undef PE_DEF_SECTION_ALIGNMENT | |
95 | #define PE_DEF_SECTION_ALIGNMENT ${OVERRIDE_SECTION_ALIGNMENT} | |
96 | #endif | |
97 | ||
98 | #ifdef TARGET_IS_i386pep | |
99 | #define DLL_SUPPORT | |
100 | #endif | |
101 | ||
102 | #if defined(TARGET_IS_i386pep) || ! defined(DLL_SUPPORT) | |
103 | #define PE_DEF_SUBSYSTEM 3 | |
a07ee50e CV |
104 | #undef NT_EXE_IMAGE_BASE |
105 | #define NT_EXE_IMAGE_BASE \ | |
106 | ((bfd_vma) (${move_default_addr_high} ? 0x100400000LL \ | |
107 | : 0x400000LL)) | |
108 | #undef NT_DLL_IMAGE_BASE | |
109 | #define NT_DLL_IMAGE_BASE \ | |
110 | ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \ | |
111 | : 0x10000000LL)) | |
112 | #undef NT_DLL_AUTO_IMAGE_BASE | |
113 | #define NT_DLL_AUTO_IMAGE_BASE \ | |
114 | ((bfd_vma) (${move_default_addr_high} ? 0x400000000LL \ | |
115 | : 0x61300000LL)) | |
116 | #undef NT_DLL_AUTO_IMAGE_MASK | |
117 | #define NT_DLL_AUTO_IMAGE_MASK \ | |
118 | ((bfd_vma) (${move_default_addr_high} ? 0x1ffff0000LL \ | |
119 | : 0x0ffc0000LL)) | |
99ad8390 NC |
120 | #else |
121 | #undef NT_EXE_IMAGE_BASE | |
a07ee50e CV |
122 | #define NT_EXE_IMAGE_BASE \ |
123 | ((bfd_vma) (${move_default_addr_high} ? 0x100010000LL \ | |
124 | : 0x10000LL)) | |
125 | #undef NT_DLL_IMAGE_BASE | |
126 | #define NT_DLL_IMAGE_BASE \ | |
127 | ((bfd_vma) (${move_default_addr_high} ? 0x110000000LL \ | |
128 | : 0x10000000LL)) | |
129 | #undef NT_DLL_AUTO_IMAGE_BASE | |
130 | #define NT_DLL_AUTO_IMAGE_BASE \ | |
131 | ((bfd_vma) (${move_default_addr_high} ? 0x120000000LL \ | |
132 | : 0x61300000LL)) | |
133 | #undef NT_DLL_AUTO_IMAGE_MASK | |
134 | #define NT_DLL_AUTO_IMAGE_MASK \ | |
135 | ((bfd_vma) (${move_default_addr_high} ? 0x0ffff0000LL \ | |
136 | : 0x0ffc0000LL)) | |
99ad8390 NC |
137 | #undef PE_DEF_SECTION_ALIGNMENT |
138 | #define PE_DEF_SUBSYSTEM 2 | |
139 | #undef PE_DEF_FILE_ALIGNMENT | |
140 | #define PE_DEF_FILE_ALIGNMENT 0x00000200 | |
141 | #define PE_DEF_SECTION_ALIGNMENT 0x00000400 | |
142 | #endif | |
143 | ||
99ad8390 NC |
144 | static struct internal_extra_pe_aouthdr pep; |
145 | static int dll; | |
decfa41e | 146 | static int pep_subsystem = ${SUBSYSTEM}; |
99ad8390 NC |
147 | static flagword real_flags = IMAGE_FILE_LARGE_ADDRESS_AWARE; |
148 | static int support_old_code = 0; | |
149 | static lang_assignment_statement_type *image_base_statement = 0; | |
2f563b51 | 150 | static unsigned short pe_dll_characteristics = 0; |
eeb14e5a | 151 | static bfd_boolean insert_timestamp = TRUE; |
61e2488c | 152 | static const char *emit_build_id; |
99ad8390 NC |
153 | |
154 | #ifdef DLL_SUPPORT | |
880383ca | 155 | static int pep_enable_stdcall_fixup = 1; /* 0=disable 1=enable (default). */ |
99ad8390 | 156 | static char * pep_out_def_filename = NULL; |
99ad8390 NC |
157 | static int pep_enable_auto_image_base = 0; |
158 | static char * pep_dll_search_prefix = NULL; | |
159 | #endif | |
160 | ||
161 | extern const char *output_filename; | |
162 | ||
d74720d2 | 163 | static int is_underscoring (void) |
99ad8390 | 164 | { |
d74720d2 KT |
165 | int u = 0; |
166 | if (pep_leading_underscore != -1) | |
167 | return pep_leading_underscore; | |
168 | if (!bfd_get_target_info ("${OUTPUT_FORMAT}", NULL, NULL, &u, NULL)) | |
b804e4af KT |
169 | bfd_get_target_info ("${RELOCATEABLE_OUTPUT_FORMAT}", NULL, NULL, &u, NULL); |
170 | ||
171 | if (u == -1) | |
172 | abort (); | |
d74720d2 KT |
173 | pep_leading_underscore = (u != 0 ? 1 : 0); |
174 | return pep_leading_underscore; | |
175 | } | |
b804e4af | 176 | |
d74720d2 KT |
177 | |
178 | static void | |
179 | gld_${EMULATION_NAME}_before_parse (void) | |
180 | { | |
181 | is_underscoring (); | |
99ad8390 NC |
182 | ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`); |
183 | output_filename = "${EXECUTABLE_NAME:-a.exe}"; | |
184 | #ifdef DLL_SUPPORT | |
66be1055 | 185 | input_flags.dynamic = TRUE; |
99ad8390 | 186 | config.has_shared = 1; |
6ec6c79c | 187 | link_info.pei386_auto_import = 1; |
6cb442d3 | 188 | link_info.pei386_runtime_pseudo_reloc = 2; /* Use by default version 2. */ |
99ad8390 NC |
189 | #endif |
190 | } | |
191 | \f | |
192 | /* PE format extra command line options. */ | |
193 | ||
194 | /* Used for setting flags in the PE header. */ | |
195 | enum options | |
196 | { | |
197 | OPTION_BASE_FILE = 300 + 1, | |
198 | OPTION_DLL, | |
199 | OPTION_FILE_ALIGNMENT, | |
200 | OPTION_IMAGE_BASE, | |
201 | OPTION_MAJOR_IMAGE_VERSION, | |
202 | OPTION_MAJOR_OS_VERSION, | |
203 | OPTION_MAJOR_SUBSYSTEM_VERSION, | |
204 | OPTION_MINOR_IMAGE_VERSION, | |
205 | OPTION_MINOR_OS_VERSION, | |
206 | OPTION_MINOR_SUBSYSTEM_VERSION, | |
207 | OPTION_SECTION_ALIGNMENT, | |
208 | OPTION_STACK, | |
209 | OPTION_SUBSYSTEM, | |
210 | OPTION_HEAP, | |
211 | OPTION_SUPPORT_OLD_CODE, | |
212 | OPTION_OUT_DEF, | |
213 | OPTION_EXPORT_ALL, | |
214 | OPTION_EXCLUDE_SYMBOLS, | |
2927aaca | 215 | OPTION_EXCLUDE_ALL_SYMBOLS, |
99ad8390 NC |
216 | OPTION_KILL_ATS, |
217 | OPTION_STDCALL_ALIASES, | |
218 | OPTION_ENABLE_STDCALL_FIXUP, | |
219 | OPTION_DISABLE_STDCALL_FIXUP, | |
99ad8390 NC |
220 | OPTION_WARN_DUPLICATE_EXPORTS, |
221 | OPTION_IMP_COMPAT, | |
222 | OPTION_ENABLE_AUTO_IMAGE_BASE, | |
223 | OPTION_DISABLE_AUTO_IMAGE_BASE, | |
224 | OPTION_DLL_SEARCH_PREFIX, | |
225 | OPTION_NO_DEFAULT_EXCLUDES, | |
226 | OPTION_DLL_ENABLE_AUTO_IMPORT, | |
227 | OPTION_DLL_DISABLE_AUTO_IMPORT, | |
228 | OPTION_ENABLE_EXTRA_PE_DEBUG, | |
229 | OPTION_EXCLUDE_LIBS, | |
230 | OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC, | |
6cb442d3 | 231 | OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC, |
e1c37eb5 | 232 | OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2, |
ce11ba6c | 233 | OPTION_EXCLUDE_MODULES_FOR_IMPLIB, |
88183869 | 234 | OPTION_USE_NUL_PREFIXED_IMPORT_TABLES, |
522f09cd KT |
235 | OPTION_NO_LEADING_UNDERSCORE, |
236 | OPTION_LEADING_UNDERSCORE, | |
88183869 | 237 | OPTION_ENABLE_LONG_SECTION_NAMES, |
2f563b51 | 238 | OPTION_DISABLE_LONG_SECTION_NAMES, |
2d5c3743 | 239 | OPTION_HIGH_ENTROPY_VA, |
2f563b51 DK |
240 | OPTION_DYNAMIC_BASE, |
241 | OPTION_FORCE_INTEGRITY, | |
242 | OPTION_NX_COMPAT, | |
243 | OPTION_NO_ISOLATION, | |
244 | OPTION_NO_SEH, | |
245 | OPTION_NO_BIND, | |
246 | OPTION_WDM_DRIVER, | |
0cb112f7 | 247 | OPTION_INSERT_TIMESTAMP, |
eeb14e5a | 248 | OPTION_NO_INSERT_TIMESTAMP, |
61e2488c | 249 | OPTION_TERMINAL_SERVER_AWARE, |
dc9bd8c9 HD |
250 | OPTION_BUILD_ID, |
251 | OPTION_ENABLE_RELOC_SECTION | |
99ad8390 NC |
252 | }; |
253 | ||
254 | static void | |
255 | gld${EMULATION_NAME}_add_options | |
256 | (int ns ATTRIBUTE_UNUSED, | |
257 | char **shortopts ATTRIBUTE_UNUSED, | |
258 | int nl, | |
259 | struct option **longopts, | |
260 | int nrl ATTRIBUTE_UNUSED, | |
261 | struct option **really_longopts ATTRIBUTE_UNUSED) | |
262 | { | |
263 | static const struct option xtra_long[] = | |
264 | { | |
265 | /* PE options */ | |
266 | {"base-file", required_argument, NULL, OPTION_BASE_FILE}, | |
267 | {"dll", no_argument, NULL, OPTION_DLL}, | |
268 | {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT}, | |
269 | {"heap", required_argument, NULL, OPTION_HEAP}, | |
270 | {"image-base", required_argument, NULL, OPTION_IMAGE_BASE}, | |
271 | {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION}, | |
272 | {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION}, | |
273 | {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION}, | |
274 | {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION}, | |
275 | {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION}, | |
276 | {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION}, | |
277 | {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT}, | |
278 | {"stack", required_argument, NULL, OPTION_STACK}, | |
279 | {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM}, | |
280 | {"support-old-code", no_argument, NULL, OPTION_SUPPORT_OLD_CODE}, | |
ce11ba6c KT |
281 | {"use-nul-prefixed-import-tables", no_argument, NULL, |
282 | OPTION_USE_NUL_PREFIXED_IMPORT_TABLES}, | |
522f09cd KT |
283 | {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE}, |
284 | {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE}, | |
99ad8390 NC |
285 | #ifdef DLL_SUPPORT |
286 | /* getopt allows abbreviations, so we do this to stop it | |
287 | from treating -o as an abbreviation for this option. */ | |
288 | {"output-def", required_argument, NULL, OPTION_OUT_DEF}, | |
289 | {"output-def", required_argument, NULL, OPTION_OUT_DEF}, | |
290 | {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL}, | |
291 | {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMBOLS}, | |
2927aaca | 292 | {"exclude-all-symbols", no_argument, NULL, OPTION_EXCLUDE_ALL_SYMBOLS}, |
99ad8390 | 293 | {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS}, |
e1c37eb5 | 294 | {"exclude-modules-for-implib", required_argument, NULL, OPTION_EXCLUDE_MODULES_FOR_IMPLIB}, |
99ad8390 NC |
295 | {"kill-at", no_argument, NULL, OPTION_KILL_ATS}, |
296 | {"add-stdcall-alias", no_argument, NULL, OPTION_STDCALL_ALIASES}, | |
297 | {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP}, | |
298 | {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP}, | |
99ad8390 NC |
299 | {"warn-duplicate-exports", no_argument, NULL, OPTION_WARN_DUPLICATE_EXPORTS}, |
300 | /* getopt() allows abbreviations, so we do this to stop it from | |
301 | treating -c as an abbreviation for these --compat-implib. */ | |
302 | {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT}, | |
303 | {"compat-implib", no_argument, NULL, OPTION_IMP_COMPAT}, | |
304 | {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE}, | |
305 | {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE}, | |
306 | {"dll-search-prefix", required_argument, NULL, OPTION_DLL_SEARCH_PREFIX}, | |
307 | {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES}, | |
308 | {"enable-auto-import", no_argument, NULL, OPTION_DLL_ENABLE_AUTO_IMPORT}, | |
309 | {"disable-auto-import", no_argument, NULL, OPTION_DLL_DISABLE_AUTO_IMPORT}, | |
310 | {"enable-extra-pep-debug", no_argument, NULL, OPTION_ENABLE_EXTRA_PE_DEBUG}, | |
311 | {"enable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC}, | |
312 | {"disable-runtime-pseudo-reloc", no_argument, NULL, OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC}, | |
6cb442d3 | 313 | {"enable-runtime-pseudo-reloc-v2", no_argument, NULL, OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2}, |
99ad8390 | 314 | #endif |
88183869 DK |
315 | {"enable-long-section-names", no_argument, NULL, OPTION_ENABLE_LONG_SECTION_NAMES}, |
316 | {"disable-long-section-names", no_argument, NULL, OPTION_DISABLE_LONG_SECTION_NAMES}, | |
2d5c3743 | 317 | {"high-entropy-va", no_argument, NULL, OPTION_HIGH_ENTROPY_VA}, |
2f563b51 DK |
318 | {"dynamicbase",no_argument, NULL, OPTION_DYNAMIC_BASE}, |
319 | {"forceinteg", no_argument, NULL, OPTION_FORCE_INTEGRITY}, | |
320 | {"nxcompat", no_argument, NULL, OPTION_NX_COMPAT}, | |
321 | {"no-isolation", no_argument, NULL, OPTION_NO_ISOLATION}, | |
322 | {"no-seh", no_argument, NULL, OPTION_NO_SEH}, | |
323 | {"no-bind", no_argument, NULL, OPTION_NO_BIND}, | |
324 | {"wdmdriver", no_argument, NULL, OPTION_WDM_DRIVER}, | |
d127ecce | 325 | {"tsaware", no_argument, NULL, OPTION_TERMINAL_SERVER_AWARE}, |
0cb112f7 | 326 | {"insert-timestamp", no_argument, NULL, OPTION_INSERT_TIMESTAMP}, |
eeb14e5a | 327 | {"no-insert-timestamp", no_argument, NULL, OPTION_NO_INSERT_TIMESTAMP}, |
61e2488c | 328 | {"build-id", optional_argument, NULL, OPTION_BUILD_ID}, |
dc9bd8c9 | 329 | {"enable-reloc-section", no_argument, NULL, OPTION_ENABLE_RELOC_SECTION}, |
d127ecce | 330 | {NULL, no_argument, NULL, 0} |
99ad8390 NC |
331 | }; |
332 | ||
d127ecce AM |
333 | *longopts |
334 | = xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long)); | |
99ad8390 NC |
335 | memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long)); |
336 | } | |
337 | ||
338 | /* PE/WIN32; added routines to get the subsystem type, heap and/or stack | |
339 | parameters which may be input from the command line. */ | |
340 | ||
341 | typedef struct | |
342 | { | |
343 | void *ptr; | |
344 | int size; | |
0ead4f8d | 345 | bfd_vma value; |
99ad8390 NC |
346 | char *symbol; |
347 | int inited; | |
522f09cd KT |
348 | /* FALSE for an assembly level symbol and TRUE for a C visible symbol. |
349 | C visible symbols can be prefixed by underscore dependent on target's | |
350 | settings. */ | |
351 | bfd_boolean is_c_symbol; | |
99ad8390 NC |
352 | } definfo; |
353 | ||
522f09cd KT |
354 | #define GET_INIT_SYMBOL_NAME(IDX) \ |
355 | (init[(IDX)].symbol \ | |
535b785f | 356 | + ((!init[(IDX)].is_c_symbol || is_underscoring () == 1) ? 0 : 1)) |
522f09cd KT |
357 | |
358 | /* Decorates the C visible symbol by underscore, if target requires. */ | |
359 | #define U(CSTR) \ | |
d74720d2 | 360 | ((is_underscoring () == 0) ? CSTR : "_" CSTR) |
522f09cd | 361 | |
61e2488c | 362 | #define D(field,symbol,def,usc) {&pep.field, sizeof (pep.field), def, symbol, 0, usc} |
99ad8390 NC |
363 | |
364 | static definfo init[] = | |
365 | { | |
366 | /* imagebase must be first */ | |
367 | #define IMAGEBASEOFF 0 | |
522f09cd | 368 | D(ImageBase,"__image_base__", NT_EXE_IMAGE_BASE, FALSE), |
99ad8390 | 369 | #define DLLOFF 1 |
522f09cd | 370 | {&dll, sizeof(dll), 0, "__dll__", 0, FALSE}, |
99ad8390 | 371 | #define MSIMAGEBASEOFF 2 |
522f09cd KT |
372 | D(ImageBase, "___ImageBase", NT_EXE_IMAGE_BASE, TRUE), |
373 | D(SectionAlignment,"__section_alignment__", PE_DEF_SECTION_ALIGNMENT, FALSE), | |
374 | D(FileAlignment,"__file_alignment__", PE_DEF_FILE_ALIGNMENT, FALSE), | |
375 | D(MajorOperatingSystemVersion,"__major_os_version__", 4, FALSE), | |
376 | D(MinorOperatingSystemVersion,"__minor_os_version__", 0, FALSE), | |
377 | D(MajorImageVersion,"__major_image_version__", 0, FALSE), | |
378 | D(MinorImageVersion,"__minor_image_version__", 0, FALSE), | |
379 | D(MajorSubsystemVersion,"__major_subsystem_version__", 5, FALSE), | |
380 | D(MinorSubsystemVersion,"__minor_subsystem_version__", 2, FALSE), | |
381 | D(Subsystem,"__subsystem__", ${SUBSYSTEM}, FALSE), | |
382 | D(SizeOfStackReserve,"__size_of_stack_reserve__", 0x200000, FALSE), | |
383 | D(SizeOfStackCommit,"__size_of_stack_commit__", 0x1000, FALSE), | |
384 | D(SizeOfHeapReserve,"__size_of_heap_reserve__", 0x100000, FALSE), | |
385 | D(SizeOfHeapCommit,"__size_of_heap_commit__", 0x1000, FALSE), | |
386 | D(LoaderFlags,"__loader_flags__", 0x0, FALSE), | |
387 | D(DllCharacteristics, "__dll_characteristics__", 0x0, FALSE), | |
388 | { NULL, 0, 0, NULL, 0, FALSE} | |
99ad8390 NC |
389 | }; |
390 | ||
391 | ||
392 | static void | |
393 | gld_${EMULATION_NAME}_list_options (FILE *file) | |
394 | { | |
395 | fprintf (file, _(" --base_file <basefile> Generate a base file for relocatable DLLs\n")); | |
396 | fprintf (file, _(" --dll Set image base to the default for DLLs\n")); | |
397 | fprintf (file, _(" --file-alignment <size> Set file alignment\n")); | |
398 | fprintf (file, _(" --heap <size> Set initial size of the heap\n")); | |
399 | fprintf (file, _(" --image-base <address> Set start address of the executable\n")); | |
400 | fprintf (file, _(" --major-image-version <number> Set version number of the executable\n")); | |
401 | fprintf (file, _(" --major-os-version <number> Set minimum required OS version\n")); | |
402 | fprintf (file, _(" --major-subsystem-version <number> Set minimum required OS subsystem version\n")); | |
403 | fprintf (file, _(" --minor-image-version <number> Set revision number of the executable\n")); | |
404 | fprintf (file, _(" --minor-os-version <number> Set minimum required OS revision\n")); | |
405 | fprintf (file, _(" --minor-subsystem-version <number> Set minimum required OS subsystem revision\n")); | |
406 | fprintf (file, _(" --section-alignment <size> Set section alignment\n")); | |
407 | fprintf (file, _(" --stack <size> Set size of the initial stack\n")); | |
408 | fprintf (file, _(" --subsystem <name>[:<version>] Set required OS subsystem [& version]\n")); | |
409 | fprintf (file, _(" --support-old-code Support interworking with old code\n")); | |
522f09cd | 410 | fprintf (file, _(" --[no-]leading-underscore Set explicit symbol underscore prefix mode\n")); |
df5f2391 | 411 | fprintf (file, _(" --[no-]insert-timestamp Use a real timestamp rather than zero (default)\n")); |
0cb112f7 | 412 | fprintf (file, _(" This makes binaries non-deterministic\n")); |
99ad8390 NC |
413 | #ifdef DLL_SUPPORT |
414 | fprintf (file, _(" --add-stdcall-alias Export symbols with and without @nn\n")); | |
415 | fprintf (file, _(" --disable-stdcall-fixup Don't link _sym to _sym@nn\n")); | |
416 | fprintf (file, _(" --enable-stdcall-fixup Link _sym to _sym@nn without warnings\n")); | |
417 | fprintf (file, _(" --exclude-symbols sym,sym,... Exclude symbols from automatic export\n")); | |
2927aaca | 418 | fprintf (file, _(" --exclude-all-symbols Exclude all symbols from automatic export\n")); |
99ad8390 | 419 | fprintf (file, _(" --exclude-libs lib,lib,... Exclude libraries from automatic export\n")); |
e1c37eb5 DK |
420 | fprintf (file, _(" --exclude-modules-for-implib mod,mod,...\n")); |
421 | fprintf (file, _(" Exclude objects, archive members from auto\n")); | |
df5f2391 | 422 | fprintf (file, _(" export, place into import library instead\n")); |
99ad8390 NC |
423 | fprintf (file, _(" --export-all-symbols Automatically export all globals to DLL\n")); |
424 | fprintf (file, _(" --kill-at Remove @nn from exported symbols\n")); | |
99ad8390 | 425 | fprintf (file, _(" --output-def <file> Generate a .DEF file for the built DLL\n")); |
df5f2391 | 426 | fprintf (file, _(" --warn-duplicate-exports Warn about duplicate exports\n")); |
99ad8390 | 427 | fprintf (file, _(" --compat-implib Create backward compatible import libs;\n\ |
df5f2391 | 428 | create __imp_<SYMBOL> as well\n")); |
99ad8390 NC |
429 | fprintf (file, _(" --enable-auto-image-base Automatically choose image base for DLLs\n\ |
430 | unless user specifies one\n")); | |
df5f2391 | 431 | fprintf (file, _(" --disable-auto-image-base Do not auto-choose image base (default)\n")); |
99ad8390 NC |
432 | fprintf (file, _(" --dll-search-prefix=<string> When linking dynamically to a dll without\n\ |
433 | an importlib, use <string><basename>.dll\n\ | |
434 | in preference to lib<basename>.dll \n")); | |
c85f715a | 435 | fprintf (file, _(" --enable-auto-import Do sophisticated linking of _sym to\n\ |
99ad8390 NC |
436 | __imp_sym for DATA references\n")); |
437 | fprintf (file, _(" --disable-auto-import Do not auto-import DATA items from DLLs\n")); | |
438 | fprintf (file, _(" --enable-runtime-pseudo-reloc Work around auto-import limitations by\n\ | |
439 | adding pseudo-relocations resolved at\n\ | |
df5f2391 | 440 | runtime\n")); |
99ad8390 | 441 | fprintf (file, _(" --disable-runtime-pseudo-reloc Do not add runtime pseudo-relocations for\n\ |
df5f2391 | 442 | auto-imported DATA\n")); |
99ad8390 NC |
443 | fprintf (file, _(" --enable-extra-pep-debug Enable verbose debug output when building\n\ |
444 | or linking to DLLs (esp. auto-import)\n")); | |
88183869 DK |
445 | fprintf (file, _(" --enable-long-section-names Use long COFF section names even in\n\ |
446 | executable image files\n")); | |
447 | fprintf (file, _(" --disable-long-section-names Never use long COFF section names, even\n\ | |
448 | in object files\n")); | |
2d5c3743 NC |
449 | fprintf (file, _(" --high-entropy-va Image is compatible with 64-bit address space\n\ |
450 | layout randomization (ASLR)\n")); | |
df5f2391 AM |
451 | fprintf (file, _(" --dynamicbase Image base address may be relocated using\n\ |
452 | address space layout randomization (ASLR)\n")); | |
dc9bd8c9 | 453 | fprintf (file, _(" --enable-reloc-section Create the base relocation table\n")); |
df5f2391 AM |
454 | fprintf (file, _(" --forceinteg Code integrity checks are enforced\n")); |
455 | fprintf (file, _(" --nxcompat Image is compatible with data execution prevention\n")); | |
456 | fprintf (file, _(" --no-isolation Image understands isolation but do not isolate the image\n")); | |
457 | fprintf (file, _(" --no-seh Image does not use SEH; no SE handler may\n\ | |
458 | be called in this image\n")); | |
459 | fprintf (file, _(" --no-bind Do not bind this image\n")); | |
460 | fprintf (file, _(" --wdmdriver Driver uses the WDM model\n")); | |
522f09cd | 461 | fprintf (file, _(" --tsaware Image is Terminal Server aware\n")); |
61e2488c | 462 | fprintf (file, _(" --build-id[=STYLE] Generate build ID\n")); |
99ad8390 NC |
463 | #endif |
464 | } | |
465 | ||
466 | ||
467 | static void | |
0ead4f8d | 468 | set_pep_name (char *name, bfd_vma val) |
99ad8390 | 469 | { |
d74720d2 KT |
470 | int i; |
471 | is_underscoring (); | |
99ad8390 NC |
472 | /* Find the name and set it. */ |
473 | for (i = 0; init[i].ptr; i++) | |
474 | { | |
522f09cd | 475 | if (strcmp (name, GET_INIT_SYMBOL_NAME (i)) == 0) |
99ad8390 NC |
476 | { |
477 | init[i].value = val; | |
478 | init[i].inited = 1; | |
479 | if (strcmp (name,"__image_base__") == 0) | |
522f09cd | 480 | set_pep_name (U ("__ImageBase"), val); |
99ad8390 NC |
481 | return; |
482 | } | |
483 | } | |
484 | abort (); | |
485 | } | |
486 | ||
decfa41e KT |
487 | static void |
488 | set_entry_point (void) | |
489 | { | |
490 | const char *entry; | |
491 | const char *initial_symbol_char; | |
d74720d2 | 492 | int i; |
decfa41e KT |
493 | |
494 | static const struct | |
6c19b93b AM |
495 | { |
496 | const int value; | |
497 | const char *entry; | |
498 | } | |
decfa41e KT |
499 | v[] = |
500 | { | |
501 | { 1, "NtProcessStartup" }, | |
502 | { 2, "WinMainCRTStartup" }, | |
503 | { 3, "mainCRTStartup" }, | |
504 | { 7, "__PosixProcessStartup" }, | |
505 | { 9, "WinMainCRTStartup" }, | |
506 | {14, "mainCRTStartup" }, | |
507 | { 0, NULL } | |
508 | }; | |
509 | ||
510 | /* Entry point name for arbitrary subsystem numbers. */ | |
511 | static const char default_entry[] = "mainCRTStartup"; | |
512 | ||
0e1862bb | 513 | if (bfd_link_pic (&link_info) || dll) |
decfa41e KT |
514 | { |
515 | entry = "DllMainCRTStartup"; | |
516 | } | |
517 | else | |
518 | { | |
519 | for (i = 0; v[i].entry; i++) | |
6c19b93b AM |
520 | if (v[i].value == pep_subsystem) |
521 | break; | |
decfa41e KT |
522 | |
523 | /* If no match, use the default. */ | |
524 | if (v[i].entry != NULL) | |
6c19b93b | 525 | entry = v[i].entry; |
decfa41e | 526 | else |
6c19b93b | 527 | entry = default_entry; |
decfa41e KT |
528 | } |
529 | ||
522f09cd | 530 | /* Now we check target's default for getting proper symbol_char. */ |
d74720d2 | 531 | initial_symbol_char = (is_underscoring () != 0 ? "_" : ""); |
522f09cd | 532 | |
decfa41e KT |
533 | if (*initial_symbol_char != '\0') |
534 | { | |
535 | char *alc_entry; | |
536 | ||
537 | /* lang_default_entry expects its argument to be permanently | |
538 | allocated, so we don't free this string. */ | |
539 | alc_entry = xmalloc (strlen (initial_symbol_char) | |
540 | + strlen (entry) | |
541 | + 1); | |
542 | strcpy (alc_entry, initial_symbol_char); | |
543 | strcat (alc_entry, entry); | |
544 | entry = alc_entry; | |
545 | } | |
546 | ||
547 | lang_default_entry (entry); | |
548 | } | |
99ad8390 NC |
549 | |
550 | static void | |
551 | set_pep_subsystem (void) | |
552 | { | |
553 | const char *sver; | |
99ad8390 NC |
554 | char *end; |
555 | int len; | |
556 | int i; | |
99ad8390 NC |
557 | unsigned long temp_subsystem; |
558 | static const struct | |
559 | { | |
560 | const char *name; | |
561 | const int value; | |
99ad8390 NC |
562 | } |
563 | v[] = | |
564 | { | |
decfa41e KT |
565 | { "native", 1 }, |
566 | { "windows", 2 }, | |
567 | { "console", 3 }, | |
568 | { "posix", 7 }, | |
569 | { "wince", 9 }, | |
570 | { "xbox", 14 }, | |
571 | { NULL, 0 } | |
99ad8390 | 572 | }; |
99ad8390 NC |
573 | |
574 | /* Check for the presence of a version number. */ | |
575 | sver = strchr (optarg, ':'); | |
576 | if (sver == NULL) | |
577 | len = strlen (optarg); | |
578 | else | |
579 | { | |
580 | len = sver - optarg; | |
581 | set_pep_name ("__major_subsystem_version__", | |
582 | strtoul (sver + 1, &end, 0)); | |
583 | if (*end == '.') | |
584 | set_pep_name ("__minor_subsystem_version__", | |
585 | strtoul (end + 1, &end, 0)); | |
586 | if (*end != '\0') | |
587 | einfo (_("%P: warning: bad version number in -subsystem option\n")); | |
588 | } | |
589 | ||
590 | /* Check for numeric subsystem. */ | |
591 | temp_subsystem = strtoul (optarg, & end, 0); | |
592 | if ((*end == ':' || *end == '\0') && (temp_subsystem < 65536)) | |
593 | { | |
594 | /* Search list for a numeric match to use its entry point. */ | |
595 | for (i = 0; v[i].name; i++) | |
596 | if (v[i].value == (int) temp_subsystem) | |
597 | break; | |
598 | ||
99ad8390 | 599 | /* Use this subsystem. */ |
decfa41e | 600 | pep_subsystem = (int) temp_subsystem; |
99ad8390 NC |
601 | } |
602 | else | |
603 | { | |
604 | /* Search for subsystem by name. */ | |
605 | for (i = 0; v[i].name; i++) | |
606 | if (strncmp (optarg, v[i].name, len) == 0 | |
607 | && v[i].name[len] == '\0') | |
608 | break; | |
609 | ||
610 | if (v[i].name == NULL) | |
611 | { | |
df5f2391 | 612 | einfo (_("%F%P: invalid subsystem type %s\n"), optarg); |
99ad8390 NC |
613 | return; |
614 | } | |
615 | ||
decfa41e | 616 | pep_subsystem = v[i].value; |
99ad8390 NC |
617 | } |
618 | ||
decfa41e | 619 | set_pep_name ("__subsystem__", pep_subsystem); |
99ad8390 NC |
620 | |
621 | return; | |
622 | } | |
623 | ||
624 | ||
625 | static void | |
626 | set_pep_value (char *name) | |
627 | { | |
628 | char *end; | |
629 | ||
0ead4f8d | 630 | set_pep_name (name, (bfd_vma) strtoull (optarg, &end, 0)); |
99ad8390 NC |
631 | |
632 | if (end == optarg) | |
df5f2391 | 633 | einfo (_("%F%P: invalid hex number for PE parameter '%s'\n"), optarg); |
99ad8390 NC |
634 | |
635 | optarg = end; | |
636 | } | |
637 | ||
638 | ||
639 | static void | |
640 | set_pep_stack_heap (char *resname, char *comname) | |
641 | { | |
642 | set_pep_value (resname); | |
643 | ||
644 | if (*optarg == ',') | |
645 | { | |
646 | optarg++; | |
647 | set_pep_value (comname); | |
648 | } | |
649 | else if (*optarg) | |
df5f2391 | 650 | einfo (_("%F%P: strange hex info for PE parameter '%s'\n"), optarg); |
99ad8390 NC |
651 | } |
652 | ||
61e2488c | 653 | #define DEFAULT_BUILD_ID_STYLE "md5" |
99ad8390 NC |
654 | |
655 | static bfd_boolean | |
656 | gld${EMULATION_NAME}_handle_option (int optc) | |
657 | { | |
d74720d2 | 658 | is_underscoring (); |
99ad8390 NC |
659 | switch (optc) |
660 | { | |
661 | default: | |
662 | return FALSE; | |
663 | ||
664 | case OPTION_BASE_FILE: | |
665 | link_info.base_file = fopen (optarg, FOPEN_WB); | |
666 | if (link_info.base_file == NULL) | |
e922bcab | 667 | einfo (_("%F%P: cannot open base file %s\n"), optarg); |
99ad8390 NC |
668 | break; |
669 | ||
670 | /* PE options. */ | |
671 | case OPTION_HEAP: | |
672 | set_pep_stack_heap ("__size_of_heap_reserve__", "__size_of_heap_commit__"); | |
673 | break; | |
674 | case OPTION_STACK: | |
675 | set_pep_stack_heap ("__size_of_stack_reserve__", "__size_of_stack_commit__"); | |
676 | break; | |
677 | case OPTION_SUBSYSTEM: | |
678 | set_pep_subsystem (); | |
679 | break; | |
680 | case OPTION_MAJOR_OS_VERSION: | |
681 | set_pep_value ("__major_os_version__"); | |
682 | break; | |
683 | case OPTION_MINOR_OS_VERSION: | |
684 | set_pep_value ("__minor_os_version__"); | |
685 | break; | |
686 | case OPTION_MAJOR_SUBSYSTEM_VERSION: | |
687 | set_pep_value ("__major_subsystem_version__"); | |
688 | break; | |
689 | case OPTION_MINOR_SUBSYSTEM_VERSION: | |
690 | set_pep_value ("__minor_subsystem_version__"); | |
691 | break; | |
692 | case OPTION_MAJOR_IMAGE_VERSION: | |
693 | set_pep_value ("__major_image_version__"); | |
694 | break; | |
695 | case OPTION_MINOR_IMAGE_VERSION: | |
696 | set_pep_value ("__minor_image_version__"); | |
697 | break; | |
698 | case OPTION_FILE_ALIGNMENT: | |
699 | set_pep_value ("__file_alignment__"); | |
700 | break; | |
701 | case OPTION_SECTION_ALIGNMENT: | |
702 | set_pep_value ("__section_alignment__"); | |
703 | break; | |
704 | case OPTION_DLL: | |
705 | set_pep_name ("__dll__", 1); | |
706 | break; | |
707 | case OPTION_IMAGE_BASE: | |
708 | set_pep_value ("__image_base__"); | |
709 | break; | |
710 | case OPTION_SUPPORT_OLD_CODE: | |
711 | support_old_code = 1; | |
712 | break; | |
ce11ba6c KT |
713 | case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES: |
714 | pep_use_nul_prefixed_import_tables = TRUE; | |
715 | break; | |
522f09cd KT |
716 | case OPTION_NO_LEADING_UNDERSCORE: |
717 | pep_leading_underscore = 0; | |
718 | break; | |
719 | case OPTION_LEADING_UNDERSCORE: | |
720 | pep_leading_underscore = 1; | |
721 | break; | |
0cb112f7 CF |
722 | case OPTION_INSERT_TIMESTAMP: |
723 | insert_timestamp = TRUE; | |
724 | break; | |
eeb14e5a LZ |
725 | case OPTION_NO_INSERT_TIMESTAMP: |
726 | insert_timestamp = FALSE; | |
727 | break; | |
99ad8390 NC |
728 | #ifdef DLL_SUPPORT |
729 | case OPTION_OUT_DEF: | |
730 | pep_out_def_filename = xstrdup (optarg); | |
731 | break; | |
732 | case OPTION_EXPORT_ALL: | |
733 | pep_dll_export_everything = 1; | |
734 | break; | |
735 | case OPTION_EXCLUDE_SYMBOLS: | |
e1c37eb5 | 736 | pep_dll_add_excludes (optarg, EXCLUDESYMS); |
99ad8390 | 737 | break; |
2927aaca NC |
738 | case OPTION_EXCLUDE_ALL_SYMBOLS: |
739 | pep_dll_exclude_all_symbols = 1; | |
740 | break; | |
99ad8390 | 741 | case OPTION_EXCLUDE_LIBS: |
e1c37eb5 DK |
742 | pep_dll_add_excludes (optarg, EXCLUDELIBS); |
743 | break; | |
744 | case OPTION_EXCLUDE_MODULES_FOR_IMPLIB: | |
745 | pep_dll_add_excludes (optarg, EXCLUDEFORIMPLIB); | |
99ad8390 NC |
746 | break; |
747 | case OPTION_KILL_ATS: | |
748 | pep_dll_kill_ats = 1; | |
749 | break; | |
750 | case OPTION_STDCALL_ALIASES: | |
751 | pep_dll_stdcall_aliases = 1; | |
752 | break; | |
753 | case OPTION_ENABLE_STDCALL_FIXUP: | |
754 | pep_enable_stdcall_fixup = 1; | |
755 | break; | |
756 | case OPTION_DISABLE_STDCALL_FIXUP: | |
757 | pep_enable_stdcall_fixup = 0; | |
758 | break; | |
99ad8390 NC |
759 | case OPTION_WARN_DUPLICATE_EXPORTS: |
760 | pep_dll_warn_dup_exports = 1; | |
761 | break; | |
762 | case OPTION_IMP_COMPAT: | |
763 | pep_dll_compat_implib = 1; | |
764 | break; | |
765 | case OPTION_ENABLE_AUTO_IMAGE_BASE: | |
766 | pep_enable_auto_image_base = 1; | |
767 | break; | |
768 | case OPTION_DISABLE_AUTO_IMAGE_BASE: | |
769 | pep_enable_auto_image_base = 0; | |
770 | break; | |
771 | case OPTION_DLL_SEARCH_PREFIX: | |
772 | pep_dll_search_prefix = xstrdup (optarg); | |
773 | break; | |
774 | case OPTION_NO_DEFAULT_EXCLUDES: | |
775 | pep_dll_do_default_excludes = 0; | |
776 | break; | |
777 | case OPTION_DLL_ENABLE_AUTO_IMPORT: | |
778 | link_info.pei386_auto_import = 1; | |
779 | break; | |
780 | case OPTION_DLL_DISABLE_AUTO_IMPORT: | |
781 | link_info.pei386_auto_import = 0; | |
782 | break; | |
783 | case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC: | |
6cb442d3 | 784 | link_info.pei386_runtime_pseudo_reloc = 2; |
99ad8390 NC |
785 | break; |
786 | case OPTION_DLL_DISABLE_RUNTIME_PSEUDO_RELOC: | |
787 | link_info.pei386_runtime_pseudo_reloc = 0; | |
788 | break; | |
6cb442d3 KT |
789 | case OPTION_DLL_ENABLE_RUNTIME_PSEUDO_RELOC_V2: |
790 | link_info.pei386_runtime_pseudo_reloc = 2; | |
791 | break; | |
99ad8390 NC |
792 | case OPTION_ENABLE_EXTRA_PE_DEBUG: |
793 | pep_dll_extra_pe_debug = 1; | |
794 | break; | |
795 | #endif | |
88183869 DK |
796 | case OPTION_ENABLE_LONG_SECTION_NAMES: |
797 | pep_use_coff_long_section_names = 1; | |
798 | break; | |
799 | case OPTION_DISABLE_LONG_SECTION_NAMES: | |
800 | pep_use_coff_long_section_names = 0; | |
801 | break; | |
2f563b51 | 802 | /* Get DLLCharacteristics bits */ |
2d5c3743 NC |
803 | case OPTION_HIGH_ENTROPY_VA: |
804 | pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA; | |
dc9bd8c9 | 805 | /* fall through */ |
2f563b51 DK |
806 | case OPTION_DYNAMIC_BASE: |
807 | pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE; | |
dc9bd8c9 HD |
808 | /* fall through */ |
809 | case OPTION_ENABLE_RELOC_SECTION: | |
810 | pep_dll_enable_reloc_section = 1; | |
2f563b51 DK |
811 | break; |
812 | case OPTION_FORCE_INTEGRITY: | |
813 | pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY; | |
814 | break; | |
815 | case OPTION_NX_COMPAT: | |
816 | pe_dll_characteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT; | |
817 | break; | |
818 | case OPTION_NO_ISOLATION: | |
819 | pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION; | |
820 | break; | |
821 | case OPTION_NO_SEH: | |
822 | pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_SEH; | |
823 | break; | |
824 | case OPTION_NO_BIND: | |
825 | pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_NO_BIND; | |
826 | break; | |
827 | case OPTION_WDM_DRIVER: | |
828 | pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_WDM_DRIVER; | |
829 | break; | |
830 | case OPTION_TERMINAL_SERVER_AWARE: | |
831 | pe_dll_characteristics |= IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE; | |
832 | break; | |
61e2488c | 833 | case OPTION_BUILD_ID: |
5e2ab612 AM |
834 | free ((char *) emit_build_id); |
835 | emit_build_id = NULL; | |
61e2488c JT |
836 | if (optarg == NULL) |
837 | optarg = DEFAULT_BUILD_ID_STYLE; | |
838 | if (strcmp (optarg, "none")) | |
839 | emit_build_id = xstrdup (optarg); | |
840 | break; | |
99ad8390 | 841 | } |
2f563b51 DK |
842 | |
843 | /* Set DLLCharacteristics bits */ | |
844 | set_pep_name ("__dll_characteristics__", pe_dll_characteristics); | |
e4492aa0 | 845 | |
99ad8390 NC |
846 | return TRUE; |
847 | } | |
848 | \f | |
849 | ||
850 | #ifdef DLL_SUPPORT | |
851 | static unsigned long | |
852 | strhash (const char *str) | |
853 | { | |
854 | const unsigned char *s; | |
855 | unsigned long hash; | |
856 | unsigned int c; | |
857 | unsigned int len; | |
858 | ||
859 | hash = 0; | |
860 | len = 0; | |
861 | s = (const unsigned char *) str; | |
862 | while ((c = *s++) != '\0') | |
863 | { | |
864 | hash += c + (c << 17); | |
865 | hash ^= hash >> 2; | |
866 | ++len; | |
867 | } | |
868 | hash += len + (len << 17); | |
869 | hash ^= hash >> 2; | |
870 | ||
871 | return hash; | |
872 | } | |
873 | ||
874 | /* Use the output file to create a image base for relocatable DLLs. */ | |
875 | ||
0ead4f8d | 876 | static bfd_vma |
99ad8390 NC |
877 | compute_dll_image_base (const char *ofile) |
878 | { | |
0ead4f8d | 879 | bfd_vma hash = (bfd_vma) strhash (ofile); |
a07ee50e | 880 | return NT_DLL_AUTO_IMAGE_BASE + ((hash << 16) & NT_DLL_AUTO_IMAGE_MASK); |
99ad8390 NC |
881 | } |
882 | #endif | |
883 | ||
884 | /* Assign values to the special symbols before the linker script is | |
885 | read. */ | |
886 | ||
887 | static void | |
888 | gld_${EMULATION_NAME}_set_symbols (void) | |
889 | { | |
890 | /* Run through and invent symbols for all the | |
891 | names and insert the defaults. */ | |
d74720d2 | 892 | int j; |
b804e4af | 893 | |
d74720d2 | 894 | is_underscoring (); |
99ad8390 NC |
895 | |
896 | if (!init[IMAGEBASEOFF].inited) | |
897 | { | |
0e1862bb | 898 | if (bfd_link_relocatable (&link_info)) |
99ad8390 | 899 | init[IMAGEBASEOFF].value = 0; |
0e1862bb | 900 | else if (init[DLLOFF].value || bfd_link_dll (&link_info)) |
d127ecce | 901 | { |
99ad8390 | 902 | #ifdef DLL_SUPPORT |
d127ecce AM |
903 | init[IMAGEBASEOFF].value = (pep_enable_auto_image_base |
904 | ? compute_dll_image_base (output_filename) | |
905 | : NT_DLL_IMAGE_BASE); | |
99ad8390 | 906 | #else |
d127ecce | 907 | init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE; |
99ad8390 | 908 | #endif |
d127ecce | 909 | } |
99ad8390 NC |
910 | else |
911 | init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE; | |
d127ecce | 912 | init[MSIMAGEBASEOFF].value = init[IMAGEBASEOFF].value; |
99ad8390 NC |
913 | } |
914 | ||
915 | /* Don't do any symbol assignments if this is a relocatable link. */ | |
0e1862bb | 916 | if (bfd_link_relocatable (&link_info)) |
99ad8390 NC |
917 | return; |
918 | ||
919 | /* Glue the assignments into the abs section. */ | |
bde18da4 | 920 | push_stat_ptr (&abs_output_section->children); |
99ad8390 NC |
921 | |
922 | for (j = 0; init[j].ptr; j++) | |
923 | { | |
0ead4f8d | 924 | bfd_vma val = init[j].value; |
99ad8390 | 925 | lang_assignment_statement_type *rv; |
522f09cd | 926 | |
2e57b2af | 927 | rv = lang_add_assignment (exp_assign (GET_INIT_SYMBOL_NAME (j), |
eb8476a6 | 928 | exp_intop (val), FALSE)); |
99ad8390 | 929 | if (init[j].size == sizeof (short)) |
0ead4f8d | 930 | *(short *) init[j].ptr = (short) val; |
99ad8390 | 931 | else if (init[j].size == sizeof (int)) |
0ead4f8d | 932 | *(int *) init[j].ptr = (int) val; |
99ad8390 | 933 | else if (init[j].size == sizeof (long)) |
0ead4f8d | 934 | *(long *) init[j].ptr = (long) val; |
99ad8390 NC |
935 | /* This might be a long long or other special type. */ |
936 | else if (init[j].size == sizeof (bfd_vma)) | |
937 | *(bfd_vma *) init[j].ptr = val; | |
938 | else abort (); | |
939 | if (j == IMAGEBASEOFF) | |
940 | image_base_statement = rv; | |
941 | } | |
942 | /* Restore the pointer. */ | |
bde18da4 | 943 | pop_stat_ptr (); |
99ad8390 NC |
944 | |
945 | if (pep.FileAlignment > pep.SectionAlignment) | |
946 | { | |
df5f2391 | 947 | einfo (_("%P: warning, file alignment > section alignment\n")); |
99ad8390 NC |
948 | } |
949 | } | |
950 | ||
951 | /* This is called after the linker script and the command line options | |
952 | have been read. */ | |
953 | ||
954 | static void | |
955 | gld_${EMULATION_NAME}_after_parse (void) | |
956 | { | |
8b747e1a DK |
957 | /* PR ld/6744: Warn the user if they have used an ELF-only |
958 | option hoping it will work on PE+. */ | |
959 | if (link_info.export_dynamic) | |
960 | einfo (_("%P: warning: --export-dynamic is not supported for PE+ " | |
961 | "targets, did you mean --export-all-symbols?\n")); | |
97b11f40 | 962 | |
decfa41e | 963 | set_entry_point (); |
e4492aa0 | 964 | |
97b11f40 | 965 | after_parse_default (); |
99ad8390 NC |
966 | } |
967 | ||
99ad8390 NC |
968 | #ifdef DLL_SUPPORT |
969 | static struct bfd_link_hash_entry *pep_undef_found_sym; | |
970 | ||
971 | static bfd_boolean | |
972 | pep_undef_cdecl_match (struct bfd_link_hash_entry *h, void *inf) | |
973 | { | |
974 | int sl; | |
975 | char *string = inf; | |
8a9eab9b | 976 | const char *hs = h->root.string; |
99ad8390 NC |
977 | |
978 | sl = strlen (string); | |
979 | if (h->type == bfd_link_hash_defined | |
8a9eab9b KT |
980 | && ((*hs == '@' && *string == '_' |
981 | && strncmp (hs + 1, string + 1, sl - 1) == 0) | |
982 | || strncmp (hs, string, sl) == 0) | |
99ad8390 NC |
983 | && h->root.string[sl] == '@') |
984 | { | |
985 | pep_undef_found_sym = h; | |
986 | return FALSE; | |
987 | } | |
988 | return TRUE; | |
989 | } | |
990 | ||
991 | static void | |
992 | pep_fixup_stdcalls (void) | |
993 | { | |
994 | static int gave_warning_message = 0; | |
995 | struct bfd_link_hash_entry *undef, *sym; | |
996 | ||
997 | if (pep_dll_extra_pe_debug) | |
998 | printf ("%s\n", __FUNCTION__); | |
999 | ||
1000 | for (undef = link_info.hash->undefs; undef; undef=undef->u.undef.next) | |
1001 | if (undef->type == bfd_link_hash_undefined) | |
1002 | { | |
1003 | char* at = strchr (undef->root.string, '@'); | |
1004 | int lead_at = (*undef->root.string == '@'); | |
8a9eab9b KT |
1005 | if (lead_at) |
1006 | at = strchr (undef->root.string + 1, '@'); | |
1007 | if (at || lead_at) | |
99ad8390 NC |
1008 | { |
1009 | /* The symbol is a stdcall symbol, so let's look for a | |
1010 | cdecl symbol with the same name and resolve to that. */ | |
8a9eab9b KT |
1011 | char *cname = xstrdup (undef->root.string); |
1012 | ||
1013 | if (lead_at) | |
1014 | *cname = '_'; | |
99ad8390 | 1015 | at = strchr (cname, '@'); |
8a9eab9b KT |
1016 | if (at) |
1017 | *at = 0; | |
99ad8390 NC |
1018 | sym = bfd_link_hash_lookup (link_info.hash, cname, 0, 0, 1); |
1019 | ||
1020 | if (sym && sym->type == bfd_link_hash_defined) | |
1021 | { | |
1022 | undef->type = bfd_link_hash_defined; | |
1023 | undef->u.def.value = sym->u.def.value; | |
1024 | undef->u.def.section = sym->u.def.section; | |
1025 | ||
1026 | if (pep_enable_stdcall_fixup == -1) | |
1027 | { | |
df5f2391 | 1028 | einfo (_("warning: resolving %s by linking to %s\n"), |
99ad8390 NC |
1029 | undef->root.string, cname); |
1030 | if (! gave_warning_message) | |
1031 | { | |
1032 | gave_warning_message = 1; | |
1033 | einfo (_("Use --enable-stdcall-fixup to disable these warnings\n")); | |
1034 | einfo (_("Use --disable-stdcall-fixup to disable these fixups\n")); | |
1035 | } | |
1036 | } | |
1037 | } | |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | /* The symbol is a cdecl symbol, so we look for stdcall | |
1042 | symbols - which means scanning the whole symbol table. */ | |
1043 | pep_undef_found_sym = 0; | |
1044 | bfd_link_hash_traverse (link_info.hash, pep_undef_cdecl_match, | |
1045 | (char *) undef->root.string); | |
1046 | sym = pep_undef_found_sym; | |
1047 | if (sym) | |
1048 | { | |
1049 | undef->type = bfd_link_hash_defined; | |
1050 | undef->u.def.value = sym->u.def.value; | |
1051 | undef->u.def.section = sym->u.def.section; | |
1052 | ||
1053 | if (pep_enable_stdcall_fixup == -1) | |
1054 | { | |
df5f2391 | 1055 | einfo (_("warning: resolving %s by linking to %s\n"), |
99ad8390 NC |
1056 | undef->root.string, sym->root.string); |
1057 | if (! gave_warning_message) | |
1058 | { | |
1059 | gave_warning_message = 1; | |
1060 | einfo (_("Use --enable-stdcall-fixup to disable these warnings\n")); | |
1061 | einfo (_("Use --disable-stdcall-fixup to disable these fixups\n")); | |
1062 | } | |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | ||
317ff008 EB |
1069 | static void |
1070 | make_import_fixup (arelent *rel, asection *s, char *name, const char *symname) | |
99ad8390 NC |
1071 | { |
1072 | struct bfd_symbol *sym = *rel->sym_ptr_ptr; | |
6cb442d3 KT |
1073 | char addend[8]; |
1074 | bfd_vma _addend = 0; | |
1075 | int suc = 0; | |
99ad8390 NC |
1076 | |
1077 | if (pep_dll_extra_pe_debug) | |
1078 | printf ("arelent: %s@%#lx: add=%li\n", sym->name, | |
0af1713e | 1079 | (unsigned long) rel->address, (long) rel->addend); |
99ad8390 | 1080 | |
6cb442d3 KT |
1081 | memset (addend, 0, sizeof (addend)); |
1082 | switch ((rel->howto->bitsize)) | |
1083 | { | |
fbea1508 AM |
1084 | case 8: |
1085 | suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1); | |
1086 | if (suc && rel->howto->pc_relative) | |
1087 | _addend = bfd_get_signed_8 (s->owner, addend); | |
1088 | else if (suc) | |
1089 | _addend = bfd_get_8 (s->owner, addend); | |
1090 | break; | |
1091 | case 16: | |
1092 | suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2); | |
1093 | if (suc && rel->howto->pc_relative) | |
1094 | _addend = bfd_get_signed_16 (s->owner, addend); | |
1095 | else if (suc) | |
1096 | _addend = bfd_get_16 (s->owner, addend); | |
1097 | break; | |
1098 | case 32: | |
1099 | suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4); | |
1100 | if (suc && rel->howto->pc_relative) | |
1101 | _addend = bfd_get_signed_32 (s->owner, addend); | |
1102 | else if (suc) | |
1103 | _addend = bfd_get_32 (s->owner, addend); | |
1104 | break; | |
1105 | case 64: | |
1106 | suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8); | |
1107 | if (suc) | |
1108 | _addend = bfd_get_64 (s->owner, addend); | |
1109 | break; | |
6cb442d3 KT |
1110 | } |
1111 | if (! suc) | |
df5f2391 | 1112 | einfo (_("%P: %C: cannot get section contents - auto-import exception\n"), |
99ad8390 NC |
1113 | s->owner, s, rel->address); |
1114 | ||
6cb442d3 KT |
1115 | if (pep_dll_extra_pe_debug) |
1116 | { | |
fbea1508 AM |
1117 | printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx", |
1118 | (long) _addend, (long) rel->addend, (long) rel->address); | |
1119 | if (rel->howto->pc_relative) | |
1120 | printf (" pcrel"); | |
1121 | printf (" %d bit rel.\n", (int) rel->howto->bitsize); | |
1122 | } | |
99ad8390 | 1123 | |
317ff008 | 1124 | pep_create_import_fixup (rel, s, _addend, name, symname); |
99ad8390 NC |
1125 | } |
1126 | ||
1127 | static bfd_boolean | |
1128 | pr_sym (struct bfd_hash_entry *h, void *inf ATTRIBUTE_UNUSED) | |
1129 | { | |
80cc85a9 | 1130 | printf ("+%s\n", h->string); |
99ad8390 NC |
1131 | |
1132 | return TRUE; | |
1133 | } | |
1134 | #endif /* DLL_SUPPORT */ | |
1135 | ||
e4492aa0 | 1136 | static void |
3efd345c DK |
1137 | debug_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj) |
1138 | { | |
1139 | int *found = (int *) obj; | |
61e2488c | 1140 | |
3efd345c DK |
1141 | if (strncmp (".debug_", sect->name, sizeof (".debug_") - 1) == 0) |
1142 | *found = 1; | |
1143 | } | |
99ad8390 | 1144 | |
61e2488c JT |
1145 | static bfd_boolean |
1146 | pecoff_checksum_contents (bfd *abfd, | |
1147 | void (*process) (const void *, size_t, void *), | |
1148 | void *arg) | |
1149 | { | |
1150 | file_ptr filepos = (file_ptr) 0; | |
1151 | ||
1152 | while (1) | |
1153 | { | |
1154 | unsigned char b; | |
1155 | int status; | |
1156 | ||
1157 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) | |
1158 | return 0; | |
1159 | ||
1160 | status = bfd_bread (&b, (bfd_size_type) 1, abfd); | |
1161 | if (status < 1) | |
6c19b93b AM |
1162 | { |
1163 | break; | |
1164 | } | |
61e2488c JT |
1165 | |
1166 | (*process) (&b, 1, arg); | |
1167 | filepos += 1; | |
1168 | } | |
1169 | ||
1170 | return TRUE; | |
1171 | } | |
1172 | ||
1173 | static bfd_boolean | |
1174 | write_build_id (bfd *abfd) | |
1175 | { | |
1176 | struct pe_tdata *t = pe_data (abfd); | |
1177 | asection *asec; | |
1178 | struct bfd_link_order *link_order = NULL; | |
1179 | unsigned char *contents; | |
1180 | bfd_size_type size; | |
1181 | bfd_size_type build_id_size; | |
1182 | unsigned char *build_id; | |
1183 | ||
74fffc39 | 1184 | /* Find the section the .buildid output section has been merged info. */ |
61e2488c JT |
1185 | for (asec = abfd->sections; asec != NULL; asec = asec->next) |
1186 | { | |
1187 | struct bfd_link_order *l = NULL; | |
1188 | for (l = asec->map_head.link_order; l != NULL; l = l->next) | |
6c19b93b AM |
1189 | { |
1190 | if (l->type == bfd_indirect_link_order) | |
1191 | { | |
1192 | if (l->u.indirect.section == t->build_id.sec) | |
1193 | { | |
1194 | link_order = l; | |
1195 | break; | |
1196 | } | |
1197 | } | |
1198 | } | |
61e2488c JT |
1199 | |
1200 | if (link_order) | |
6c19b93b | 1201 | break; |
61e2488c JT |
1202 | } |
1203 | ||
1204 | if (!link_order) | |
1205 | { | |
74fffc39 | 1206 | einfo (_("%P: warning: .buildid section discarded," |
df5f2391 | 1207 | " --build-id ignored\n")); |
61e2488c JT |
1208 | return TRUE; |
1209 | } | |
1210 | ||
1211 | if (t->build_id.sec->contents == NULL) | |
1212 | t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size); | |
1213 | contents = t->build_id.sec->contents; | |
1214 | size = t->build_id.sec->size; | |
1215 | ||
1216 | build_id_size = compute_build_id_size (t->build_id.style); | |
1217 | build_id = xmalloc (build_id_size); | |
1218 | generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size); | |
1219 | ||
1220 | bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase; | |
1221 | ||
1222 | /* Construct a debug directory entry which points to an immediately following CodeView record. */ | |
1223 | struct internal_IMAGE_DEBUG_DIRECTORY idd; | |
1224 | idd.Characteristics = 0; | |
1225 | idd.TimeDateStamp = 0; | |
1226 | idd.MajorVersion = 0; | |
1227 | idd.MinorVersion = 0; | |
1228 | idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW; | |
1229 | idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1; | |
1230 | idd.AddressOfRawData = asec->vma - ib + link_order->offset | |
1231 | + sizeof (struct external_IMAGE_DEBUG_DIRECTORY); | |
1232 | idd.PointerToRawData = asec->filepos + link_order->offset | |
1233 | + sizeof (struct external_IMAGE_DEBUG_DIRECTORY); | |
1234 | ||
1235 | struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents; | |
1236 | _bfd_XXi_swap_debugdir_out (abfd, &idd, ext); | |
1237 | ||
1238 | /* Write the debug directory enttry */ | |
1239 | if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0) | |
1240 | return 0; | |
1241 | ||
b6518b38 | 1242 | if (bfd_bwrite (contents, size, abfd) != size) |
61e2488c JT |
1243 | return 0; |
1244 | ||
1245 | /* Construct the CodeView record. */ | |
1246 | CODEVIEW_INFO cvinfo; | |
1247 | cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE; | |
1248 | cvinfo.Age = 1; | |
1249 | ||
1250 | /* Zero pad or truncate the generated build_id to fit in the CodeView record. */ | |
1251 | memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH); | |
1252 | memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH) | |
1253 | ? CV_INFO_SIGNATURE_LENGTH : build_id_size); | |
1254 | ||
1255 | free (build_id); | |
1256 | ||
1257 | /* Write the codeview record. */ | |
1258 | if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0) | |
1259 | return 0; | |
1260 | ||
1261 | /* Record the location of the debug directory in the data directory. */ | |
1262 | pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress | |
1263 | = asec->vma - ib + link_order->offset; | |
1264 | pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size | |
1265 | = sizeof (struct external_IMAGE_DEBUG_DIRECTORY); | |
1266 | ||
1267 | return TRUE; | |
1268 | } | |
1269 | ||
74fffc39 | 1270 | /* Make .buildid section, and set up coff_tdata->build_id. */ |
61e2488c JT |
1271 | static bfd_boolean |
1272 | setup_build_id (bfd *ibfd) | |
1273 | { | |
1274 | asection *s; | |
1275 | flagword flags; | |
1276 | ||
1277 | if (!validate_build_id_style (emit_build_id)) | |
1278 | { | |
df5f2391 | 1279 | einfo (_("%P: warning: unrecognized --build-id style ignored\n")); |
61e2488c JT |
1280 | return FALSE; |
1281 | } | |
1282 | ||
1283 | flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY | |
1284 | | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA); | |
74fffc39 | 1285 | s = bfd_make_section_anyway_with_flags (ibfd, ".buildid", flags); |
61e2488c JT |
1286 | if (s != NULL) |
1287 | { | |
1288 | struct pe_tdata *t = pe_data (link_info.output_bfd); | |
1289 | t->build_id.after_write_object_contents = &write_build_id; | |
1290 | t->build_id.style = emit_build_id; | |
1291 | t->build_id.sec = s; | |
1292 | ||
1293 | /* Section is a fixed size: | |
1294 | One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW, | |
1295 | pointing at a CV_INFO_PDB70 record containing the build-id, with a | |
1296 | null byte for PdbFileName. */ | |
1297 | s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY) | |
1298 | + sizeof (CV_INFO_PDB70) + 1; | |
1299 | ||
1300 | return TRUE; | |
1301 | } | |
1302 | ||
df5f2391 AM |
1303 | einfo (_("%P: warning: cannot create .buildid section," |
1304 | " --build-id ignored\n")); | |
61e2488c JT |
1305 | return FALSE; |
1306 | } | |
1307 | ||
99ad8390 NC |
1308 | static void |
1309 | gld_${EMULATION_NAME}_after_open (void) | |
1310 | { | |
5c3049d2 AM |
1311 | after_open_default (); |
1312 | ||
d74720d2 | 1313 | is_underscoring (); |
99ad8390 NC |
1314 | #ifdef DLL_SUPPORT |
1315 | if (pep_dll_extra_pe_debug) | |
1316 | { | |
1317 | bfd *a; | |
1318 | struct bfd_link_hash_entry *sym; | |
1319 | ||
1320 | printf ("%s()\n", __FUNCTION__); | |
1321 | ||
1322 | for (sym = link_info.hash->undefs; sym; sym=sym->u.undef.next) | |
1323 | printf ("-%s\n", sym->root.string); | |
1324 | bfd_hash_traverse (&link_info.hash->table, pr_sym, NULL); | |
1325 | ||
c72f2fb2 | 1326 | for (a = link_info.input_bfds; a; a = a->link.next) |
607b4833 | 1327 | printf ("*%s\n", bfd_get_filename (a)); |
99ad8390 NC |
1328 | } |
1329 | #endif | |
1330 | ||
61e2488c JT |
1331 | if (emit_build_id != NULL) |
1332 | { | |
1333 | bfd *abfd; | |
1334 | ||
1335 | /* Find a COFF input. */ | |
1336 | for (abfd = link_info.input_bfds; | |
c72f2fb2 | 1337 | abfd != (bfd *) NULL; abfd = abfd->link.next) |
61e2488c JT |
1338 | if (bfd_get_flavour (abfd) == bfd_target_coff_flavour) |
1339 | break; | |
1340 | ||
1341 | /* If there are no COFF input files do not try to | |
1342 | add a build-id section. */ | |
1343 | if (abfd == NULL | |
1344 | || !setup_build_id (abfd)) | |
1345 | { | |
1346 | free ((char *) emit_build_id); | |
1347 | emit_build_id = NULL; | |
1348 | } | |
1349 | } | |
1350 | ||
99ad8390 NC |
1351 | /* Pass the wacky PE command line options into the output bfd. |
1352 | FIXME: This should be done via a function, rather than by | |
1353 | including an internal BFD header. */ | |
1354 | ||
eabf307f AM |
1355 | if (bfd_get_flavour (link_info.output_bfd) != bfd_target_coff_flavour |
1356 | || coff_data (link_info.output_bfd) == NULL | |
f13a99db | 1357 | || coff_data (link_info.output_bfd)->pe == 0) |
df5f2391 | 1358 | einfo (_("%F%P: cannot perform PE operations on non PE output file '%pB'\n"), |
f13a99db | 1359 | link_info.output_bfd); |
99ad8390 | 1360 | |
f13a99db AM |
1361 | pe_data (link_info.output_bfd)->pe_opthdr = pep; |
1362 | pe_data (link_info.output_bfd)->dll = init[DLLOFF].value; | |
1363 | pe_data (link_info.output_bfd)->real_flags |= real_flags; | |
00386881 NC |
1364 | if (insert_timestamp) |
1365 | pe_data (link_info.output_bfd)->timestamp = -1; | |
1366 | else | |
1367 | pe_data (link_info.output_bfd)->timestamp = 0; | |
3efd345c DK |
1368 | |
1369 | /* At this point we must decide whether to use long section names | |
1370 | in the output or not. If the user hasn't explicitly specified | |
1371 | on the command line, we leave it to the default for the format | |
1372 | (object files yes, image files no), except if there is debug | |
1373 | information present; GDB relies on the long section names to | |
1374 | find it, so enable it in that case. */ | |
1375 | if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none) | |
1376 | { | |
0e1862bb | 1377 | if (bfd_link_relocatable (&link_info)) |
61e2488c JT |
1378 | pep_use_coff_long_section_names = 1; |
1379 | else | |
3efd345c | 1380 | { |
61e2488c JT |
1381 | /* Iterate over all sections of all input BFDs, checking |
1382 | for any that begin 'debug_' and are long names. */ | |
1383 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
1384 | { | |
1385 | int found_debug = 0; | |
1386 | ||
1387 | bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug); | |
1388 | if (found_debug) | |
1389 | { | |
1390 | pep_use_coff_long_section_names = 1; | |
1391 | break; | |
1392 | } | |
1393 | } | |
3efd345c DK |
1394 | } |
1395 | } | |
1396 | ||
88183869 | 1397 | pep_output_file_set_long_section_names (link_info.output_bfd); |
99ad8390 NC |
1398 | |
1399 | #ifdef DLL_SUPPORT | |
f13a99db | 1400 | pep_process_import_defs (link_info.output_bfd, &link_info); |
99ad8390 | 1401 | |
317ff008 EB |
1402 | if (link_info.pei386_auto_import) /* -1=warn or 1=enable */ |
1403 | pep_find_data_imports (U ("_head_"), make_import_fixup); | |
99ad8390 | 1404 | |
317ff008 EB |
1405 | /* The implementation of the feature is rather dumb and would cause the |
1406 | compilation time to go through the roof if there are many undefined | |
1407 | symbols in the link, so it needs to be run after auto-import. */ | |
1408 | if (pep_enable_stdcall_fixup) /* -1=warn or 1=enable */ | |
8a9eab9b KT |
1409 | pep_fixup_stdcalls (); |
1410 | ||
99ad8390 | 1411 | #ifndef TARGET_IS_i386pep |
0e1862bb | 1412 | if (bfd_link_pic (&link_info)) |
99ad8390 | 1413 | #else |
0e1862bb | 1414 | if (!bfd_link_relocatable (&link_info)) |
99ad8390 | 1415 | #endif |
f13a99db | 1416 | pep_dll_build_sections (link_info.output_bfd, &link_info); |
99ad8390 NC |
1417 | |
1418 | #ifndef TARGET_IS_i386pep | |
1419 | else | |
f13a99db | 1420 | pep_exe_build_sections (link_info.output_bfd, &link_info); |
99ad8390 NC |
1421 | #endif |
1422 | #endif /* DLL_SUPPORT */ | |
1423 | ||
1424 | { | |
1425 | /* This next chunk of code tries to detect the case where you have | |
1426 | two import libraries for the same DLL (specifically, | |
1427 | symbolically linking libm.a and libc.a in cygwin to | |
1428 | libcygwin.a). In those cases, it's possible for function | |
1429 | thunks from the second implib to be used but without the | |
1430 | head/tail objects, causing an improper import table. We detect | |
1431 | those cases and rename the "other" import libraries to match | |
1432 | the one the head/tail come from, so that the linker will sort | |
1433 | things nicely and produce a valid import table. */ | |
1434 | ||
1435 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
1436 | { | |
1437 | if (is->the_bfd->my_archive) | |
1438 | { | |
1439 | int idata2 = 0, reloc_count=0, is_imp = 0; | |
1440 | asection *sec; | |
1441 | ||
1442 | /* See if this is an import library thunk. */ | |
1443 | for (sec = is->the_bfd->sections; sec; sec = sec->next) | |
1444 | { | |
1445 | if (strcmp (sec->name, ".idata\$2") == 0) | |
1446 | idata2 = 1; | |
1447 | if (CONST_STRNEQ (sec->name, ".idata\$")) | |
1448 | is_imp = 1; | |
1449 | reloc_count += sec->reloc_count; | |
1450 | } | |
1451 | ||
1452 | if (is_imp && !idata2 && reloc_count) | |
1453 | { | |
1454 | /* It is, look for the reference to head and see if it's | |
1455 | from our own library. */ | |
1456 | for (sec = is->the_bfd->sections; sec; sec = sec->next) | |
1457 | { | |
1458 | int i; | |
99ad8390 NC |
1459 | long relsize; |
1460 | asymbol **symbols; | |
1461 | arelent **relocs; | |
1462 | int nrelocs; | |
1463 | ||
99ad8390 NC |
1464 | relsize = bfd_get_reloc_upper_bound (is->the_bfd, sec); |
1465 | if (relsize < 1) | |
1466 | break; | |
1467 | ||
5c1d2f5f | 1468 | if (!bfd_generic_link_read_symbols (is->the_bfd)) |
99ad8390 | 1469 | { |
df5f2391 | 1470 | einfo (_("%F%P: %pB: could not read symbols: %E\n"), |
5c1d2f5f | 1471 | is->the_bfd); |
99ad8390 NC |
1472 | return; |
1473 | } | |
5c1d2f5f | 1474 | symbols = bfd_get_outsymbols (is->the_bfd); |
99ad8390 NC |
1475 | |
1476 | relocs = xmalloc ((size_t) relsize); | |
1477 | nrelocs = bfd_canonicalize_reloc (is->the_bfd, sec, | |
1478 | relocs, symbols); | |
1479 | if (nrelocs < 0) | |
1480 | { | |
1481 | free (relocs); | |
d003af55 | 1482 | einfo (_("%X%P: unable to process relocs: %E\n")); |
99ad8390 NC |
1483 | return; |
1484 | } | |
1485 | ||
1486 | for (i = 0; i < nrelocs; i++) | |
1487 | { | |
1488 | struct bfd_symbol *s; | |
1489 | struct bfd_link_hash_entry * blhe; | |
b16c44de | 1490 | const char *other_bfd_filename; |
99ad8390 NC |
1491 | |
1492 | s = (relocs[i]->sym_ptr_ptr)[0]; | |
1493 | ||
1494 | if (s->flags & BSF_LOCAL) | |
1495 | continue; | |
1496 | ||
1497 | /* Thunk section with reloc to another bfd. */ | |
1498 | blhe = bfd_link_hash_lookup (link_info.hash, | |
1499 | s->name, | |
1500 | FALSE, FALSE, TRUE); | |
1501 | ||
1502 | if (blhe == NULL | |
1503 | || blhe->type != bfd_link_hash_defined) | |
1504 | continue; | |
1505 | ||
1506 | other_bfd_filename | |
1507 | = blhe->u.def.section->owner->my_archive | |
1508 | ? bfd_get_filename (blhe->u.def.section->owner->my_archive) | |
1509 | : bfd_get_filename (blhe->u.def.section->owner); | |
1510 | ||
42627821 KT |
1511 | if (filename_cmp (bfd_get_filename |
1512 | (is->the_bfd->my_archive), | |
1513 | other_bfd_filename) == 0) | |
99ad8390 NC |
1514 | continue; |
1515 | ||
1516 | /* Rename this implib to match the other one. */ | |
7b958a48 AM |
1517 | if (!bfd_set_filename (is->the_bfd->my_archive, |
1518 | other_bfd_filename)) | |
1519 | einfo ("%F%P: %pB: %E\n", is->the_bfd); | |
99ad8390 NC |
1520 | } |
1521 | ||
1522 | free (relocs); | |
1523 | /* Note - we do not free the symbols, | |
1524 | they are now cached in the BFD. */ | |
1525 | } | |
1526 | } | |
1527 | } | |
1528 | } | |
1529 | } | |
1530 | ||
1531 | { | |
1532 | int is_ms_arch = 0; | |
1533 | bfd *cur_arch = 0; | |
1534 | lang_input_statement_type *is2; | |
1535 | lang_input_statement_type *is3; | |
1536 | ||
1537 | /* Careful - this is a shell script. Watch those dollar signs! */ | |
1538 | /* Microsoft import libraries have every member named the same, | |
1539 | and not in the right order for us to link them correctly. We | |
1540 | must detect these and rename the members so that they'll link | |
1541 | correctly. There are three types of objects: the head, the | |
1542 | thunks, and the sentinel(s). The head is easy; it's the one | |
1543 | with idata2. We assume that the sentinels won't have relocs, | |
1544 | and the thunks will. It's easier than checking the symbol | |
1545 | table for external references. */ | |
1546 | LANG_FOR_EACH_INPUT_STATEMENT (is) | |
1547 | { | |
1548 | if (is->the_bfd->my_archive) | |
1549 | { | |
1550 | char *pnt; | |
1551 | bfd *arch = is->the_bfd->my_archive; | |
1552 | ||
1553 | if (cur_arch != arch) | |
1554 | { | |
1555 | cur_arch = arch; | |
1556 | is_ms_arch = 1; | |
1557 | ||
1558 | for (is3 = is; | |
1559 | is3 && is3->the_bfd->my_archive == arch; | |
1560 | is3 = (lang_input_statement_type *) is3->next) | |
1561 | { | |
1562 | /* A MS dynamic import library can also contain static | |
1563 | members, so look for the first element with a .dll | |
1564 | extension, and use that for the remainder of the | |
1565 | comparisons. */ | |
607b4833 | 1566 | pnt = strrchr (bfd_get_filename (is3->the_bfd), '.'); |
42627821 | 1567 | if (pnt != NULL && filename_cmp (pnt, ".dll") == 0) |
99ad8390 NC |
1568 | break; |
1569 | } | |
1570 | ||
1571 | if (is3 == NULL) | |
1572 | is_ms_arch = 0; | |
1573 | else | |
1574 | { | |
1575 | /* OK, found one. Now look to see if the remaining | |
1576 | (dynamic import) members use the same name. */ | |
1577 | for (is2 = is; | |
1578 | is2 && is2->the_bfd->my_archive == arch; | |
1579 | is2 = (lang_input_statement_type *) is2->next) | |
1580 | { | |
1581 | /* Skip static members, ie anything with a .obj | |
1582 | extension. */ | |
607b4833 | 1583 | pnt = strrchr (bfd_get_filename (is2->the_bfd), '.'); |
42627821 | 1584 | if (pnt != NULL && filename_cmp (pnt, ".obj") == 0) |
99ad8390 NC |
1585 | continue; |
1586 | ||
607b4833 AM |
1587 | if (filename_cmp (bfd_get_filename (is3->the_bfd), |
1588 | bfd_get_filename (is2->the_bfd))) | |
99ad8390 NC |
1589 | { |
1590 | is_ms_arch = 0; | |
1591 | break; | |
1592 | } | |
1593 | } | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | /* This fragment might have come from an .obj file in a Microsoft | |
1598 | import, and not an actual import record. If this is the case, | |
1599 | then leave the filename alone. */ | |
607b4833 | 1600 | pnt = strrchr (bfd_get_filename (is->the_bfd), '.'); |
99ad8390 | 1601 | |
42627821 | 1602 | if (is_ms_arch && (filename_cmp (pnt, ".dll") == 0)) |
99ad8390 NC |
1603 | { |
1604 | int idata2 = 0, reloc_count=0; | |
1605 | asection *sec; | |
1606 | char *new_name, seq; | |
1607 | ||
1608 | for (sec = is->the_bfd->sections; sec; sec = sec->next) | |
1609 | { | |
1610 | if (strcmp (sec->name, ".idata\$2") == 0) | |
1611 | idata2 = 1; | |
1612 | reloc_count += sec->reloc_count; | |
1613 | } | |
1614 | ||
1615 | if (idata2) /* .idata2 is the TOC */ | |
1616 | seq = 'a'; | |
1617 | else if (reloc_count > 0) /* thunks */ | |
1618 | seq = 'b'; | |
1619 | else /* sentinel */ | |
1620 | seq = 'c'; | |
1621 | ||
7b958a48 AM |
1622 | new_name |
1623 | = xmalloc (strlen (bfd_get_filename (is->the_bfd)) + 3); | |
1624 | sprintf (new_name, "%s.%c", | |
1625 | bfd_get_filename (is->the_bfd), seq); | |
1626 | is->filename = bfd_set_filename (is->the_bfd, new_name); | |
1627 | free (new_name); | |
1628 | if (!is->filename) | |
1629 | einfo ("%F%P: %pB: %E\n", is->the_bfd); | |
99ad8390 NC |
1630 | } |
1631 | } | |
1632 | } | |
1633 | } | |
1634 | } | |
1635 | \f | |
1636 | static void | |
1637 | gld_${EMULATION_NAME}_before_allocation (void) | |
1638 | { | |
d74720d2 | 1639 | is_underscoring (); |
99ad8390 NC |
1640 | before_allocation_default (); |
1641 | } | |
1642 | \f | |
1643 | #ifdef DLL_SUPPORT | |
1644 | /* This is called when an input file isn't recognized as a BFD. We | |
1645 | check here for .DEF files and pull them in automatically. */ | |
1646 | ||
1647 | static int | |
1648 | saw_option (char *option) | |
1649 | { | |
1650 | int i; | |
1651 | ||
d74720d2 KT |
1652 | is_underscoring (); |
1653 | ||
99ad8390 | 1654 | for (i = 0; init[i].ptr; i++) |
522f09cd | 1655 | if (strcmp (GET_INIT_SYMBOL_NAME (i), option) == 0) |
99ad8390 NC |
1656 | return init[i].inited; |
1657 | return 0; | |
1658 | } | |
1659 | #endif /* DLL_SUPPORT */ | |
1660 | ||
1661 | static bfd_boolean | |
1662 | gld_${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED) | |
1663 | { | |
1664 | #ifdef DLL_SUPPORT | |
1665 | const char *ext = entry->filename + strlen (entry->filename) - 4; | |
1666 | ||
42627821 | 1667 | if (filename_cmp (ext, ".def") == 0 || filename_cmp (ext, ".DEF") == 0) |
99ad8390 NC |
1668 | { |
1669 | pep_def_file = def_file_parse (entry->filename, pep_def_file); | |
1670 | ||
1671 | if (pep_def_file) | |
1672 | { | |
1673 | int i, buflen=0, len; | |
1674 | char *buf; | |
1675 | ||
1676 | for (i = 0; i < pep_def_file->num_exports; i++) | |
1677 | { | |
1678 | len = strlen (pep_def_file->exports[i].internal_name); | |
1679 | if (buflen < len + 2) | |
1680 | buflen = len + 2; | |
1681 | } | |
1682 | ||
1683 | buf = xmalloc (buflen); | |
1684 | ||
1685 | for (i = 0; i < pep_def_file->num_exports; i++) | |
1686 | { | |
1687 | struct bfd_link_hash_entry *h; | |
1688 | ||
522f09cd KT |
1689 | sprintf (buf, "%s%s", U (""), |
1690 | pep_def_file->exports[i].internal_name); | |
99ad8390 NC |
1691 | |
1692 | h = bfd_link_hash_lookup (link_info.hash, buf, TRUE, TRUE, TRUE); | |
1693 | if (h == (struct bfd_link_hash_entry *) NULL) | |
df5f2391 | 1694 | einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n")); |
99ad8390 NC |
1695 | if (h->type == bfd_link_hash_new) |
1696 | { | |
1697 | h->type = bfd_link_hash_undefined; | |
1698 | h->u.undef.abfd = NULL; | |
1699 | bfd_link_add_undef (link_info.hash, h); | |
1700 | } | |
1701 | } | |
1702 | free (buf); | |
1703 | ||
1704 | /* def_file_print (stdout, pep_def_file); */ | |
1705 | if (pep_def_file->is_dll == 1) | |
64d94ba0 | 1706 | link_info.type = type_dll; |
99ad8390 NC |
1707 | |
1708 | if (pep_def_file->base_address != (bfd_vma)(-1)) | |
1709 | { | |
f13a99db AM |
1710 | pep.ImageBase |
1711 | = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase | |
1712 | = init[IMAGEBASEOFF].value | |
1713 | = pep_def_file->base_address; | |
99ad8390 NC |
1714 | init[IMAGEBASEOFF].inited = 1; |
1715 | if (image_base_statement) | |
eb8476a6 MR |
1716 | image_base_statement->exp |
1717 | = exp_assign ("__image_base__", exp_intop (pep.ImageBase), | |
1718 | FALSE); | |
99ad8390 NC |
1719 | } |
1720 | ||
1721 | if (pep_def_file->stack_reserve != -1 | |
1722 | && ! saw_option ("__size_of_stack_reserve__")) | |
1723 | { | |
1724 | pep.SizeOfStackReserve = pep_def_file->stack_reserve; | |
1725 | if (pep_def_file->stack_commit != -1) | |
1726 | pep.SizeOfStackCommit = pep_def_file->stack_commit; | |
1727 | } | |
1728 | if (pep_def_file->heap_reserve != -1 | |
1729 | && ! saw_option ("__size_of_heap_reserve__")) | |
1730 | { | |
1731 | pep.SizeOfHeapReserve = pep_def_file->heap_reserve; | |
1732 | if (pep_def_file->heap_commit != -1) | |
1733 | pep.SizeOfHeapCommit = pep_def_file->heap_commit; | |
1734 | } | |
1735 | return TRUE; | |
1736 | } | |
1737 | } | |
1738 | #endif | |
1739 | return FALSE; | |
1740 | } | |
1741 | ||
1742 | static bfd_boolean | |
1743 | gld_${EMULATION_NAME}_recognized_file (lang_input_statement_type *entry ATTRIBUTE_UNUSED) | |
1744 | { | |
d74720d2 | 1745 | is_underscoring (); |
99ad8390 NC |
1746 | #ifdef DLL_SUPPORT |
1747 | #ifdef TARGET_IS_i386pep | |
1748 | pep_dll_id_target ("pei-x86-64"); | |
1749 | #endif | |
880383ca NC |
1750 | if (pep_bfd_is_dll (entry->the_bfd)) |
1751 | return pep_implied_import_dll (entry->filename); | |
99ad8390 NC |
1752 | #endif |
1753 | return FALSE; | |
1754 | } | |
1755 | ||
1756 | static void | |
1757 | gld_${EMULATION_NAME}_finish (void) | |
1758 | { | |
d74720d2 | 1759 | is_underscoring (); |
99ad8390 NC |
1760 | finish_default (); |
1761 | ||
1762 | #ifdef DLL_SUPPORT | |
0e1862bb | 1763 | if (bfd_link_pic (&link_info) |
dc9bd8c9 | 1764 | || pep_dll_enable_reloc_section |
0e1862bb L |
1765 | || (!bfd_link_relocatable (&link_info) |
1766 | && pep_def_file->num_exports != 0)) | |
99ad8390 | 1767 | { |
f13a99db | 1768 | pep_dll_fill_sections (link_info.output_bfd, &link_info); |
76359541 TP |
1769 | if (command_line.out_implib_filename) |
1770 | pep_dll_generate_implib (pep_def_file, | |
1771 | command_line.out_implib_filename, &link_info); | |
99ad8390 NC |
1772 | } |
1773 | ||
1774 | if (pep_out_def_filename) | |
1775 | pep_dll_generate_def_file (pep_out_def_filename); | |
1776 | #endif /* DLL_SUPPORT */ | |
1777 | ||
1778 | /* I don't know where .idata gets set as code, but it shouldn't be. */ | |
1779 | { | |
f13a99db | 1780 | asection *asec = bfd_get_section_by_name (link_info.output_bfd, ".idata"); |
99ad8390 NC |
1781 | |
1782 | if (asec) | |
1783 | { | |
1784 | asec->flags &= ~SEC_CODE; | |
1785 | asec->flags |= SEC_DATA; | |
1786 | } | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | \f | |
1791 | /* Place an orphan section. | |
1792 | ||
1793 | We use this to put sections in a reasonable place in the file, and | |
1794 | to ensure that they are aligned as required. | |
1795 | ||
d34c1c51 | 1796 | We handle grouped sections here as well. A section named .foo\$nn |
99ad8390 NC |
1797 | goes into the output section .foo. All grouped sections are sorted |
1798 | by name. | |
1799 | ||
1800 | Grouped sections for the default sections are handled by the | |
1801 | default linker script using wildcards, and are sorted by | |
1802 | sort_sections. */ | |
1803 | ||
c2edb4b8 | 1804 | static lang_output_section_statement_type * |
8a99a385 AM |
1805 | gld_${EMULATION_NAME}_place_orphan (asection *s, |
1806 | const char *secname, | |
1807 | int constraint) | |
99ad8390 | 1808 | { |
bcacc0f5 | 1809 | const char *orig_secname = secname; |
99ad8390 NC |
1810 | char *dollar = NULL; |
1811 | lang_output_section_statement_type *os; | |
1812 | lang_statement_list_type add_child; | |
b5f14a6d | 1813 | lang_output_section_statement_type *match_by_name = NULL; |
011aa75f | 1814 | lang_statement_union_type **pl; |
99ad8390 | 1815 | |
99ad8390 | 1816 | /* Look through the script to see where to place this section. */ |
0e1862bb | 1817 | if (!bfd_link_relocatable (&link_info) |
d34c1c51 | 1818 | && (dollar = strchr (secname, '\$')) != NULL) |
99ad8390 | 1819 | { |
bcacc0f5 | 1820 | size_t len = dollar - secname; |
99ad8390 | 1821 | char *newname = xmalloc (len + 1); |
bcacc0f5 | 1822 | memcpy (newname, secname, len); |
99ad8390 NC |
1823 | newname[len] = '\0'; |
1824 | secname = newname; | |
1825 | } | |
1826 | ||
99ad8390 NC |
1827 | lang_list_init (&add_child); |
1828 | ||
d127ecce AM |
1829 | os = NULL; |
1830 | if (constraint == 0) | |
1831 | for (os = lang_output_section_find (secname); | |
1832 | os != NULL; | |
1833 | os = next_matching_output_section_statement (os, 0)) | |
1834 | { | |
1835 | /* If we don't match an existing output section, tell | |
1836 | lang_insert_orphan to create a new output section. */ | |
1837 | constraint = SPECIAL; | |
1838 | ||
1839 | if (os->bfd_section != NULL | |
1840 | && (os->bfd_section->flags == 0 | |
1841 | || ((s->flags ^ os->bfd_section->flags) | |
1842 | & (SEC_LOAD | SEC_ALLOC)) == 0)) | |
1843 | { | |
1844 | /* We already have an output section statement with this | |
1845 | name, and its bfd section has compatible flags. | |
1846 | If the section already exists but does not have any flags set, | |
1847 | then it has been created by the linker, probably as a result of | |
1848 | a --section-start command line switch. */ | |
b9c361e0 | 1849 | lang_add_section (&add_child, s, NULL, os); |
d127ecce AM |
1850 | break; |
1851 | } | |
b5f14a6d DD |
1852 | |
1853 | /* Save unused output sections in case we can match them | |
1854 | against orphans later. */ | |
1855 | if (os->bfd_section == NULL) | |
1856 | match_by_name = os; | |
d127ecce AM |
1857 | } |
1858 | ||
b5f14a6d DD |
1859 | /* If we didn't match an active output section, see if we matched an |
1860 | unused one and use that. */ | |
1861 | if (os == NULL && match_by_name) | |
1862 | { | |
b9c361e0 | 1863 | lang_add_section (&match_by_name->children, s, NULL, match_by_name); |
b5f14a6d DD |
1864 | return match_by_name; |
1865 | } | |
1866 | ||
d127ecce | 1867 | if (os == NULL) |
99ad8390 NC |
1868 | { |
1869 | static struct orphan_save hold[] = | |
1870 | { | |
1871 | { ".text", | |
1872 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE, | |
1873 | 0, 0, 0, 0 }, | |
d4874973 KT |
1874 | { ".idata", |
1875 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1876 | 0, 0, 0, 0 }, | |
99ad8390 NC |
1877 | { ".rdata", |
1878 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, | |
1879 | 0, 0, 0, 0 }, | |
1880 | { ".data", | |
1881 | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA, | |
1882 | 0, 0, 0, 0 }, | |
1883 | { ".bss", | |
1884 | SEC_ALLOC, | |
1885 | 0, 0, 0, 0 } | |
1886 | }; | |
1887 | enum orphan_save_index | |
1888 | { | |
1889 | orphan_text = 0, | |
d4874973 | 1890 | orphan_idata, |
99ad8390 NC |
1891 | orphan_rodata, |
1892 | orphan_data, | |
1893 | orphan_bss | |
1894 | }; | |
1895 | static int orphan_init_done = 0; | |
1896 | struct orphan_save *place; | |
1897 | lang_output_section_statement_type *after; | |
1898 | etree_type *address; | |
93638471 AM |
1899 | flagword flags; |
1900 | asection *nexts; | |
99ad8390 NC |
1901 | |
1902 | if (!orphan_init_done) | |
1903 | { | |
1904 | struct orphan_save *ho; | |
1905 | for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho) | |
1906 | if (ho->name != NULL) | |
1907 | { | |
1908 | ho->os = lang_output_section_find (ho->name); | |
1909 | if (ho->os != NULL && ho->os->flags == 0) | |
1910 | ho->os->flags = ho->flags; | |
1911 | } | |
1912 | orphan_init_done = 1; | |
1913 | } | |
1914 | ||
523f4c92 L |
1915 | flags = s->flags; |
1916 | if (!bfd_link_relocatable (&link_info)) | |
1917 | { | |
1918 | nexts = s; | |
1919 | while ((nexts = bfd_get_next_section_by_name (nexts->owner, | |
1920 | nexts))) | |
1921 | if (nexts->output_section == NULL | |
1922 | && (nexts->flags & SEC_EXCLUDE) == 0 | |
1923 | && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0 | |
1924 | && (nexts->owner->flags & DYNAMIC) == 0 | |
00f93c44 | 1925 | && !bfd_input_just_syms (nexts->owner)) |
523f4c92 L |
1926 | flags = (((flags ^ SEC_READONLY) |
1927 | | (nexts->flags ^ SEC_READONLY)) | |
1928 | ^ SEC_READONLY); | |
1929 | } | |
1930 | ||
99ad8390 NC |
1931 | /* Try to put the new output section in a reasonable place based |
1932 | on the section name and section flags. */ | |
1933 | ||
1934 | place = NULL; | |
93638471 | 1935 | if ((flags & SEC_ALLOC) == 0) |
99ad8390 | 1936 | ; |
93638471 | 1937 | else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
99ad8390 | 1938 | place = &hold[orphan_bss]; |
93638471 | 1939 | else if ((flags & SEC_READONLY) == 0) |
99ad8390 | 1940 | place = &hold[orphan_data]; |
93638471 | 1941 | else if ((flags & SEC_CODE) == 0) |
d4874973 KT |
1942 | { |
1943 | place = (!strncmp (secname, ".idata\$", 7) ? &hold[orphan_idata] | |
1944 | : &hold[orphan_rodata]); | |
1945 | } | |
99ad8390 NC |
1946 | else |
1947 | place = &hold[orphan_text]; | |
1948 | ||
1949 | after = NULL; | |
1950 | if (place != NULL) | |
1951 | { | |
1952 | if (place->os == NULL) | |
1953 | place->os = lang_output_section_find (place->name); | |
1954 | after = place->os; | |
1955 | if (after == NULL) | |
93638471 AM |
1956 | after = lang_output_section_find_by_flags (s, flags, &place->os, |
1957 | NULL); | |
99ad8390 NC |
1958 | if (after == NULL) |
1959 | /* *ABS* is always the first output section statement. */ | |
8ce18f9c | 1960 | after = (void *) lang_os_list.head; |
99ad8390 NC |
1961 | } |
1962 | ||
4bf0cafb DK |
1963 | /* All sections in an executable must be aligned to a page boundary. |
1964 | In a relocatable link, just preserve the incoming alignment; the | |
1965 | address is discarded by lang_insert_orphan in that case, anyway. */ | |
99ad8390 | 1966 | address = exp_unop (ALIGN_K, exp_nameop (NAME, "__section_alignment__")); |
8a99a385 AM |
1967 | os = lang_insert_orphan (s, secname, constraint, after, place, address, |
1968 | &add_child); | |
0e1862bb | 1969 | if (bfd_link_relocatable (&link_info)) |
4bf0cafb | 1970 | { |
3d9c8f6b | 1971 | os->section_alignment = exp_intop (1U << s->alignment_power); |
4bf0cafb DK |
1972 | os->bfd_section->alignment_power = s->alignment_power; |
1973 | } | |
99ad8390 NC |
1974 | } |
1975 | ||
011aa75f AM |
1976 | /* If the section name has a '\$', sort it with the other '\$' |
1977 | sections. */ | |
1978 | for (pl = &os->children.head; *pl != NULL; pl = &(*pl)->header.next) | |
1979 | { | |
1980 | lang_input_section_type *ls; | |
1981 | const char *lname; | |
99ad8390 | 1982 | |
011aa75f AM |
1983 | if ((*pl)->header.type != lang_input_section_enum) |
1984 | continue; | |
99ad8390 | 1985 | |
011aa75f | 1986 | ls = &(*pl)->input_section; |
99ad8390 | 1987 | |
fd361982 | 1988 | lname = bfd_section_name (ls->section); |
011aa75f AM |
1989 | if (strchr (lname, '\$') != NULL |
1990 | && (dollar == NULL || strcmp (orig_secname, lname) < 0)) | |
1991 | break; | |
1992 | } | |
99ad8390 | 1993 | |
011aa75f AM |
1994 | if (add_child.head != NULL) |
1995 | { | |
1996 | *add_child.tail = *pl; | |
1997 | *pl = add_child.head; | |
1998 | } | |
99ad8390 | 1999 | |
c2edb4b8 | 2000 | return os; |
99ad8390 NC |
2001 | } |
2002 | ||
2003 | static bfd_boolean | |
2004 | gld_${EMULATION_NAME}_open_dynamic_archive | |
2005 | (const char *arch ATTRIBUTE_UNUSED, | |
2006 | search_dirs_type *search, | |
2007 | lang_input_statement_type *entry) | |
2008 | { | |
2009 | static const struct | |
2010 | { | |
2011 | const char * format; | |
2012 | bfd_boolean use_prefix; | |
2013 | } | |
2014 | libname_fmt [] = | |
2015 | { | |
2016 | /* Preferred explicit import library for dll's. */ | |
2017 | { "lib%s.dll.a", FALSE }, | |
2018 | /* Alternate explicit import library for dll's. */ | |
2019 | { "%s.dll.a", FALSE }, | |
2020 | /* "libfoo.a" could be either an import lib or a static lib. | |
6c19b93b AM |
2021 | For backwards compatibility, libfoo.a needs to precede |
2022 | libfoo.dll and foo.dll in the search. */ | |
99ad8390 | 2023 | { "lib%s.a", FALSE }, |
92b93329 | 2024 | /* The 'native' spelling of an import lib name is "foo.lib". */ |
99ad8390 | 2025 | { "%s.lib", FALSE }, |
f6c4d4b1 NC |
2026 | /* PR 22948 - Check for an import library. */ |
2027 | { "lib%s.lib", FALSE }, | |
99ad8390 NC |
2028 | #ifdef DLL_SUPPORT |
2029 | /* Try "<prefix>foo.dll" (preferred dll name, if specified). */ | |
2030 | { "%s%s.dll", TRUE }, | |
2031 | #endif | |
2032 | /* Try "libfoo.dll" (default preferred dll name). */ | |
2033 | { "lib%s.dll", FALSE }, | |
2034 | /* Finally try 'native' dll name "foo.dll". */ | |
2035 | { "%s.dll", FALSE }, | |
2036 | /* Note: If adding more formats to this table, make sure to check to | |
2037 | see if their length is longer than libname_fmt[0].format, and if | |
2038 | so, update the call to xmalloc() below. */ | |
2039 | { NULL, FALSE } | |
2040 | }; | |
2041 | static unsigned int format_max_len = 0; | |
2042 | const char * filename; | |
2043 | char * full_string; | |
2044 | char * base_string; | |
2045 | unsigned int i; | |
2046 | ||
2047 | ||
d4ae5fb0 | 2048 | if (! entry->flags.maybe_archive || entry->flags.full_name_provided) |
99ad8390 NC |
2049 | return FALSE; |
2050 | ||
2051 | filename = entry->filename; | |
2052 | ||
2053 | if (format_max_len == 0) | |
2054 | /* We need to allow space in the memory that we are going to allocate | |
2055 | for the characters in the format string. Since the format array is | |
2056 | static we only need to calculate this information once. In theory | |
2057 | this value could also be computed statically, but this introduces | |
2058 | the possibility for a discrepancy and hence a possible memory | |
2059 | corruption. The lengths we compute here will be too long because | |
2060 | they will include any formating characters (%s) in the strings, but | |
2061 | this will not matter. */ | |
2062 | for (i = 0; libname_fmt[i].format; i++) | |
2063 | if (format_max_len < strlen (libname_fmt[i].format)) | |
2064 | format_max_len = strlen (libname_fmt[i].format); | |
2065 | ||
2066 | full_string = xmalloc (strlen (search->name) | |
2067 | + strlen (filename) | |
2068 | + format_max_len | |
2069 | #ifdef DLL_SUPPORT | |
2070 | + (pep_dll_search_prefix | |
2071 | ? strlen (pep_dll_search_prefix) : 0) | |
2072 | #endif | |
2073 | /* Allow for the terminating NUL and for the path | |
2074 | separator character that is inserted between | |
2075 | search->name and the start of the format string. */ | |
2076 | + 2); | |
2077 | ||
2078 | sprintf (full_string, "%s/", search->name); | |
2079 | base_string = full_string + strlen (full_string); | |
2080 | ||
2081 | for (i = 0; libname_fmt[i].format; i++) | |
2082 | { | |
92b93329 | 2083 | #ifdef DLL_SUPPORT |
99ad8390 NC |
2084 | if (libname_fmt[i].use_prefix) |
2085 | { | |
2086 | if (!pep_dll_search_prefix) | |
2087 | continue; | |
2088 | sprintf (base_string, libname_fmt[i].format, pep_dll_search_prefix, filename); | |
2089 | } | |
2090 | else | |
2091 | #endif | |
2092 | sprintf (base_string, libname_fmt[i].format, filename); | |
2093 | ||
2094 | if (ldfile_try_open_bfd (full_string, entry)) | |
2095 | break; | |
2096 | } | |
2097 | ||
2098 | if (!libname_fmt[i].format) | |
2099 | { | |
2100 | free (full_string); | |
2101 | return FALSE; | |
2102 | } | |
2103 | ||
2104 | entry->filename = full_string; | |
2105 | ||
2106 | return TRUE; | |
2107 | } | |
2108 | ||
2109 | static int | |
2110 | gld_${EMULATION_NAME}_find_potential_libraries | |
2111 | (char *name, lang_input_statement_type *entry) | |
2112 | { | |
2113 | return ldfile_open_file_search (name, entry, "", ".lib"); | |
2114 | } | |
2115 | \f | |
2116 | static char * | |
2117 | gld_${EMULATION_NAME}_get_script (int *isfile) | |
2118 | EOF | |
2119 | # Scripts compiled in. | |
2120 | # sed commands to quote an ld script as a C string. | |
2121 | sc="-f stringify.sed" | |
2122 | ||
92b93329 | 2123 | fragment <<EOF |
99ad8390 NC |
2124 | { |
2125 | *isfile = 0; | |
2126 | ||
0e1862bb | 2127 | if (bfd_link_relocatable (&link_info) && config.build_constructors) |
99ad8390 NC |
2128 | return |
2129 | EOF | |
2130 | sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c | |
0e1862bb | 2131 | echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c |
99ad8390 NC |
2132 | sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c |
2133 | echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c | |
2134 | sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c | |
2135 | echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c | |
2136 | sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c | |
e2a83dd0 | 2137 | if test -n "$GENERATE_AUTO_IMPORT_SCRIPT" ; then |
6ec6c79c | 2138 | echo ' ; else if (link_info.pei386_auto_import == 1 && link_info.pei386_runtime_pseudo_reloc != 2) return' >> e${EMULATION_NAME}.c |
e2a83dd0 NC |
2139 | sed $sc ldscripts/${EMULATION_NAME}.xa >> e${EMULATION_NAME}.c |
2140 | fi | |
99ad8390 NC |
2141 | echo ' ; else return' >> e${EMULATION_NAME}.c |
2142 | sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c | |
2143 | echo '; }' >> e${EMULATION_NAME}.c | |
2144 | ||
92b93329 | 2145 | fragment <<EOF |
99ad8390 NC |
2146 | |
2147 | ||
2148 | struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = | |
2149 | { | |
2150 | gld_${EMULATION_NAME}_before_parse, | |
2151 | syslib_default, | |
2152 | hll_default, | |
2153 | gld_${EMULATION_NAME}_after_parse, | |
2154 | gld_${EMULATION_NAME}_after_open, | |
5c3261b0 | 2155 | after_check_relocs_default, |
9b538ba7 | 2156 | before_place_orphans_default, |
99ad8390 NC |
2157 | after_allocation_default, |
2158 | set_output_arch_default, | |
2159 | ldemul_default_target, | |
2160 | gld_${EMULATION_NAME}_before_allocation, | |
2161 | gld_${EMULATION_NAME}_get_script, | |
2162 | "${EMULATION_NAME}", | |
2163 | "${OUTPUT_FORMAT}", | |
2164 | gld_${EMULATION_NAME}_finish, | |
2165 | NULL, /* Create output section statements. */ | |
2166 | gld_${EMULATION_NAME}_open_dynamic_archive, | |
2167 | gld_${EMULATION_NAME}_place_orphan, | |
2168 | gld_${EMULATION_NAME}_set_symbols, | |
2169 | NULL, /* parse_args */ | |
2170 | gld${EMULATION_NAME}_add_options, | |
2171 | gld${EMULATION_NAME}_handle_option, | |
2172 | gld_${EMULATION_NAME}_unrecognized_file, | |
2173 | gld_${EMULATION_NAME}_list_options, | |
2174 | gld_${EMULATION_NAME}_recognized_file, | |
2175 | gld_${EMULATION_NAME}_find_potential_libraries, | |
7a2f2d82 | 2176 | NULL, /* new_vers_pattern. */ |
1ff6de03 NA |
2177 | NULL, /* extra_map_file_text */ |
2178 | ${LDEMUL_EMIT_CTF_EARLY-NULL}, | |
3edf7b9f DR |
2179 | ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}, |
2180 | ${LDEMUL_PRINT_SYMBOL-NULL} | |
99ad8390 NC |
2181 | }; |
2182 | EOF |