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