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