Print this page
7323 ld(1) -zignore can erroneously discard init and fini arrays as unreferenced
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/libld/common/place.c
+++ new/usr/src/cmd/sgs/libld/common/place.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1988 AT&T
24 24 * All Rights Reserved
25 25 *
26 26 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
27 27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 28 */
29 29
30 30 /*
31 31 * Map file parsing and input section to output segment mapping.
32 32 */
33 33 #include <stdio.h>
34 34 #include <string.h>
35 35 #include <debug.h>
36 36 #include "msg.h"
37 37 #include "_libld.h"
38 38
39 39 /*
40 40 * Each time a section is placed, the function set_addralign()
41 41 * is called. This function performs:
42 42 *
43 43 * - if the section is from an external file, check if this is empty or not.
44 44 * If not, we know the segment this section will belong needs a program
45 45 * header. (Of course, the program is needed only if this section falls
46 46 * into a loadable segment.)
47 47 * - compute the Least Common Multiplier for setting the segment alignment.
48 48 */
49 49 static void
50 50 set_addralign(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp)
51 51 {
52 52 Shdr *shdr = isp->is_shdr;
53 53
54 54 /* A discarded section has no influence on the output */
55 55 if (isp->is_flags & FLG_IS_DISCARD)
56 56 return;
57 57
58 58 /*
59 59 * If this section has data or will be assigned data
60 60 * later, mark this segment not-empty.
61 61 */
62 62 if ((shdr->sh_size != 0) ||
63 63 ((isp->is_flags & FLG_IS_EXTERNAL) == 0))
64 64 osp->os_sgdesc->sg_flags |= FLG_SG_PHREQ;
65 65
66 66 if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) &&
67 67 (osp->os_sgdesc->sg_phdr).p_type != PT_LOAD)
68 68 return;
69 69
70 70 osp->os_sgdesc->sg_align =
71 71 ld_lcm(osp->os_sgdesc->sg_align, shdr->sh_addralign);
72 72 }
73 73
74 74 /*
75 75 * Return the first input descriptor for a given output descriptor,
76 76 * or NULL if there are none.
77 77 */
78 78
79 79 Is_desc *
80 80 ld_os_first_isdesc(Os_desc *osp)
81 81 {
82 82 int i;
83 83
84 84 for (i = 0; i < OS_ISD_NUM; i++) {
85 85 APlist *ap_isdesc = osp->os_isdescs[i];
86 86
87 87 if (aplist_nitems(ap_isdesc) > 0)
88 88 return ((Is_desc *)ap_isdesc->apl_data[0]);
89 89 }
90 90
91 91 return (NULL);
92 92 }
93 93
94 94 /*
95 95 * Attach an input section to an output section
96 96 *
97 97 * entry:
98 98 * ofl - File descriptor
99 99 * osp - Output section descriptor
100 100 * isp - Input section descriptor
101 101 * mapfile_sort - True (1) if segment supports mapfile specified ordering
102 102 * of otherwise unordered input sections, and False (0) otherwise.
103 103 *
104 104 * exit:
105 105 * - The input section has been attached to the output section
106 106 * - If the input section is a candidate for string table merging,
107 107 * then it is appended to the output section's list of merge
108 108 * candidates (os_mstridescs).
109 109 *
110 110 * On success, returns True (1). On failure, False (0).
111 111 */
112 112 static int
113 113 os_attach_isp(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp, int mapfile_sort)
114 114 {
115 115 Aliste init_arritems;
116 116 int os_isdescs_idx, do_append = 1;
117 117
118 118 if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
119 119 init_arritems = AL_CNT_OS_ISDESCS;
120 120 os_isdescs_idx = OS_ISD_DEFAULT;
121 121
122 122 /*
123 123 * If section ordering was specified for an unordered section
124 124 * via the mapfile, then search in the OS_ISD_DEFAULT list
125 125 * and insert it in the specified position. Ordered sections
126 126 * are placed in ascending order before unordered sections
127 127 * (sections with an is_ordndx value of zero).
128 128 *
129 129 * If no mapfile ordering was specified, we append it in
130 130 * the usual way below.
131 131 */
132 132 if (mapfile_sort && (isp->is_ordndx > 0)) {
133 133 APlist *ap_isdesc = osp->os_isdescs[OS_ISD_DEFAULT];
134 134 Aliste idx2;
135 135 Is_desc *isp2;
136 136
137 137 for (APLIST_TRAVERSE(ap_isdesc, idx2, isp2)) {
138 138 if (isp2->is_ordndx &&
139 139 (isp2->is_ordndx <= isp->is_ordndx))
140 140 continue;
141 141
142 142 if (aplist_insert(
143 143 &osp->os_isdescs[OS_ISD_DEFAULT],
144 144 isp, init_arritems, idx2) == NULL)
145 145 return (0);
146 146 do_append = 0;
147 147 break;
148 148 }
149 149 }
150 150 } else { /* Ordered section (via shdr flags) */
151 151 Word shndx;
152 152
153 153 /* SHF_ORDERED uses sh_info, SHF_LINK_ORDERED uses sh_link */
154 154 shndx = (isp->is_shdr->sh_flags & SHF_ORDERED) ?
155 155 isp->is_shdr->sh_info : isp->is_shdr->sh_link;
156 156
157 157 if (shndx == SHN_BEFORE) {
158 158 init_arritems = AL_CNT_OS_ISDESCS_BA;
159 159 os_isdescs_idx = OS_ISD_BEFORE;
160 160 } else if (shndx == SHN_AFTER) {
161 161 init_arritems = AL_CNT_OS_ISDESCS_BA;
162 162 os_isdescs_idx = OS_ISD_AFTER;
163 163 } else {
164 164 init_arritems = AL_CNT_OS_ISDESCS;
165 165 os_isdescs_idx = OS_ISD_ORDERED;
166 166 }
167 167 }
168 168
169 169 /*
170 170 * If we didn't insert a section into the default list using
171 171 * mapfile specified ordering above, then append the input
172 172 * section to the appropriate list.
173 173 */
174 174 if (do_append && aplist_append(&(osp->os_isdescs[os_isdescs_idx]),
175 175 isp, init_arritems) == NULL)
176 176 return (0);
177 177 isp->is_osdesc = osp;
178 178
179 179 /*
180 180 * A section can be merged if the following are true:
181 181 * - The SHF_MERGE|SHF_STRINGS flags must be set
182 182 * - String table compression must not be disabled (-znocompstrtab)
183 183 * - Mapfile ordering must not have been used.
184 184 * - The section must not be ordered via section header flags.
185 185 * - It must not be the generated section being built to
186 186 * replace the sections on this list.
187 187 */
188 188 if (((isp->is_shdr->sh_flags & (SHF_MERGE | SHF_STRINGS)) !=
189 189 (SHF_MERGE | SHF_STRINGS)) ||
190 190 ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0) ||
191 191 !do_append ||
192 192 ((isp->is_flags & (FLG_IS_ORDERED | FLG_IS_GNSTRMRG)) != 0))
193 193 return (1);
194 194
195 195 /*
196 196 * Skip sections with (sh_entsize > 1) or (sh_addralign > 1).
197 197 *
198 198 * sh_entsize:
199 199 * We are currently only able to merge string tables containing
200 200 * strings with 1-byte (char) characters. Support for wide
201 201 * characters will require our string table compression code
202 202 * to be extended to handle larger character sizes.
203 203 *
204 204 * sh_addralign:
205 205 * Alignments greater than 1 would require our string table
206 206 * compression code to insert null bytes to move each
207 207 * string to the required alignment.
208 208 */
209 209 if ((isp->is_shdr->sh_entsize > 1) ||
210 210 (isp->is_shdr->sh_addralign > 1)) {
211 211 DBG_CALL(Dbg_sec_unsup_strmerge(ofl->ofl_lml, isp));
212 212 return (1);
213 213 }
214 214
215 215 if (aplist_append(&osp->os_mstrisdescs, isp,
216 216 AL_CNT_OS_MSTRISDESCS) == NULL)
217 217 return (0);
218 218
219 219 /*
220 220 * The SHF_MERGE|SHF_STRINGS flags tell us that the program that
221 221 * created the section intended it to be mergeable. The
222 222 * FLG_IS_INSTRMRG flag says that we have done validity testing
223 223 * and decided that it is safe to act on that hint.
224 224 */
225 225 isp->is_flags |= FLG_IS_INSTRMRG;
226 226
227 227 return (1);
228 228 }
229 229
230 230 /*
231 231 * Determine whether this input COMDAT section already exists for the associated
232 232 * output section. If so, then discard this input section. Otherwise, this
233 233 * must be the first COMDAT section, thus it is kept for future comparisons.
234 234 */
235 235 static uintptr_t
236 236 add_comdat(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp)
237 237 {
238 238 Isd_node isd, *isdp;
239 239 avl_tree_t *avlt;
240 240 avl_index_t where;
241 241 Group_desc *gr;
242 242
243 243 /*
244 244 * Sections to which COMDAT groups apply are FLG_IS_COMDAT but are
245 245 * discarded separately by the group logic so should never be
246 246 * discarded here.
247 247 */
248 248 if ((isp->is_shdr->sh_flags & SHF_GROUP) &&
249 249 ((gr = ld_get_group(ofl, isp)) != NULL) &&
250 250 (gr->gd_data[0] & GRP_COMDAT))
251 251 return (1);
252 252
253 253 /*
254 254 * Create a COMDAT avl tree for this output section if required.
255 255 */
256 256 if ((avlt = osp->os_comdats) == NULL) {
257 257 if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
258 258 return (S_ERROR);
259 259 avl_create(avlt, isdavl_compare, sizeof (Isd_node),
260 260 SGSOFFSETOF(Isd_node, isd_avl));
261 261 osp->os_comdats = avlt;
262 262 }
263 263
264 264 /*
265 265 * A standard COMDAT section uses the section name as search key.
266 266 */
267 267 isd.isd_name = isp->is_name;
268 268 isd.isd_hash = sgs_str_hash(isd.isd_name);
269 269
270 270 if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
271 271 isp->is_osdesc = osp;
272 272
273 273 /*
274 274 * If this section hasn't already been identified as discarded,
275 275 * generate a suitable diagnostic.
276 276 */
277 277 if ((isp->is_flags & FLG_IS_DISCARD) == 0) {
278 278 isp->is_flags |= FLG_IS_DISCARD;
279 279 isp->is_comdatkeep = isdp->isd_isp;
280 280 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp,
281 281 isdp->isd_isp));
282 282 }
283 283
284 284 /*
285 285 * A discarded section does not require assignment to an output
286 286 * section. However, if relaxed relocations have been enabled
287 287 * (either from -z relaxreloc, or asserted with .gnu.linkonce
288 288 * processing), then this section must still be assigned to an
289 289 * output section so that the sloppy relocation logic will have
290 290 * the information necessary to do its work.
291 291 */
292 292 return (0);
293 293 }
294 294
295 295 /*
296 296 * This is a new COMDAT section - so keep it.
297 297 */
298 298 if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL)
299 299 return (S_ERROR);
300 300
301 301 isdp->isd_name = isd.isd_name;
302 302 isdp->isd_hash = isd.isd_hash;
303 303 isdp->isd_isp = isp;
304 304
305 305 avl_insert(avlt, isdp, where);
306 306 return (1);
307 307 }
308 308
309 309 /*
310 310 * Determine whether a GNU group COMDAT section name follows the convention
311 311 *
312 312 * section-name.symbol-name
313 313 *
314 314 * Each section within the input file is compared to see if the full section
315 315 * name matches the beginning of the COMDAT section, with a following '.'.
316 316 * A pointer to the symbol name, starting with the '.' is returned so that the
317 317 * caller can strip off the required section name.
318 318 */
319 319 static char *
320 320 gnu_comdat_sym(Ifl_desc *ifl, Is_desc *gisp)
321 321 {
322 322 size_t ndx;
323 323
324 324 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
325 325 Is_desc *isp;
326 326 size_t ssize;
327 327
328 328 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
329 329 (isp == gisp) || (isp->is_name == NULL))
330 330 continue;
331 331
332 332 /*
333 333 * It's questionable whether this size should be cached in the
334 334 * Is_desc. However, this seems an infrequent operation and
335 335 * adding Is_desc members can escalate memory usage for large
336 336 * link-edits. For now, size the section name dynamically.
337 337 */
338 338 ssize = strlen(isp->is_name);
339 339 if ((strncmp(isp->is_name, gisp->is_name, ssize) == 0) &&
340 340 (gisp->is_name[ssize] == '.'))
341 341 return ((char *)&gisp->is_name[ssize]);
342 342 }
343 343 return (NULL);
344 344 }
345 345
346 346 /*
347 347 * GNU .gnu.linkonce sections follow a naming convention that indicates the
348 348 * required association with an output section. Determine whether this input
349 349 * section follows the convention, and if so return the appropriate output
350 350 * section name.
351 351 *
352 352 * .gnu.linkonce.b.* -> .bss
353 353 * .gnu.linkonce.d.* -> .data
354 354 * .gnu.linkonce.l.* -> .ldata
355 355 * .gnu.linkonce.lb.* -> .lbss
356 356 * .gnu.linkonce.lr.* -> .lrodata
357 357 * .gnu.linkonce.r.* -> .rodata
358 358 * .gnu.linkonce.s.* -> .sdata
359 359 * .gnu.linkonce.s2.* -> .sdata2
360 360 * .gnu.linkonce.sb.* -> .sbss
361 361 * .gnu.linkonce.sb2.* -> .sbss2
362 362 * .gnu.linkonce.t.* -> .text
363 363 * .gnu.linkonce.tb.* -> .tbss
364 364 * .gnu.linkonce.td.* -> .tdata
365 365 * .gnu.linkonce.wi.* -> .debug_info
366 366 */
367 367 #define NSTR_CH1(ch) (*(nstr + 1) == (ch))
368 368 #define NSTR_CH2(ch) (*(nstr + 2) == (ch))
369 369 #define NSTR_CH3(ch) (*(nstr + 3) == (ch))
370 370
371 371 static const char *
372 372 gnu_linkonce_sec(const char *ostr)
373 373 {
374 374 const char *nstr = &ostr[MSG_SCN_GNU_LINKONCE_SIZE];
375 375
376 376 switch (*nstr) {
377 377 case 'b':
378 378 if (NSTR_CH1('.'))
379 379 return (MSG_ORIG(MSG_SCN_BSS));
380 380 break;
381 381 case 'd':
382 382 if (NSTR_CH1('.'))
383 383 return (MSG_ORIG(MSG_SCN_DATA));
384 384 break;
385 385 case 'l':
386 386 if (NSTR_CH1('.'))
387 387 return (MSG_ORIG(MSG_SCN_LDATA));
388 388 else if (NSTR_CH1('b') && NSTR_CH2('.'))
389 389 return (MSG_ORIG(MSG_SCN_LBSS));
390 390 else if (NSTR_CH1('r') && NSTR_CH2('.'))
391 391 return (MSG_ORIG(MSG_SCN_LRODATA));
392 392 break;
393 393 case 'r':
394 394 if (NSTR_CH1('.'))
395 395 return (MSG_ORIG(MSG_SCN_RODATA));
396 396 break;
397 397 case 's':
398 398 if (NSTR_CH1('.'))
399 399 return (MSG_ORIG(MSG_SCN_SDATA));
400 400 else if (NSTR_CH1('2') && NSTR_CH2('.'))
401 401 return (MSG_ORIG(MSG_SCN_SDATA2));
402 402 else if (NSTR_CH1('b') && NSTR_CH2('.'))
403 403 return (MSG_ORIG(MSG_SCN_SBSS));
404 404 else if (NSTR_CH1('b') && NSTR_CH2('2') && NSTR_CH3('.'))
405 405 return (MSG_ORIG(MSG_SCN_SBSS2));
406 406 break;
407 407 case 't':
408 408 if (NSTR_CH1('.'))
409 409 return (MSG_ORIG(MSG_SCN_TEXT));
410 410 else if (NSTR_CH1('b') && NSTR_CH2('.'))
411 411 return (MSG_ORIG(MSG_SCN_TBSS));
412 412 else if (NSTR_CH1('d') && NSTR_CH2('.'))
413 413 return (MSG_ORIG(MSG_SCN_TDATA));
414 414 break;
415 415 case 'w':
416 416 if (NSTR_CH1('i') && NSTR_CH2('.'))
417 417 return (MSG_ORIG(MSG_SCN_DEBUG_INFO));
418 418 break;
419 419 default:
420 420 break;
421 421 }
422 422
423 423 /*
424 424 * No special name match found.
425 425 */
426 426 return (ostr);
427 427 }
428 428 #undef NSTR_CH1
429 429 #undef NSTR_CH2
430 430 #undef NSTR_CH3
431 431
432 432 /*
433 433 * The GNU link-editor maps sections generated by the GNU compiler separately
434 434 * due to -ffunction-sections, -fdata-sections or for other reasons into the
435 435 * "normal" section represented.
436 436 *
437 437 * Sections are named .<main>.<symbol> where <main> is the usual section to
438 438 * which it should be mapped, and <symbol> is providing the unique name for
439 439 * the original section. Both parts of the name may contain periods, in cases
440 440 * where the unique part of the name contains a '.' and/or the section it
441 441 * contributes to does (such as .data.rel.ro)
442 442 *
443 443 * .rodata.str* and .rodata.cst* are mapped to .rodata.
444 444 *
445 445 * As a further complication, the GNU link-editor may or may not merge
446 446 * .ctors.* and .dtors.* into init_array and fini_array, rather than ctors and
447 447 * dtors. We do not implement this at this time.
448 448 *
449 449 * The GNU link editor may also arrange for sections with .local in their name
450 450 * to be mapped as above, but grouped together. We do not implement this (and
451 451 * do not merge them at all, to make this clear)
452 452 *
453 453 * This table is processed in order. Longer mappings must come first.
454 454 */
455 455 static struct split_sec_mapping {
456 456 char *leader;
457 457 char *section;
458 458 boolean_t precise;
459 459 } split_sec_mapping[] = {
460 460 { ".bss.", ".bss", B_FALSE },
461 461 { ".ctors.", ".ctors", B_FALSE },
462 462 { ".data.rel.local.", ".data.rel.local", B_FALSE },
463 463 { ".data.rel.local", ".data.rel.local", B_TRUE },
464 464 { ".data.rel.ro.local.", ".data.rel.ro", B_FALSE },
465 465 { ".data.rel.ro.", ".data.rel.ro", B_FALSE },
466 466 { ".data.rel.ro", ".data.rel.ro", B_TRUE },
467 467 { ".data.rel.", ".data.rel", B_FALSE },
468 468 { ".data.rel", ".data.rel", B_TRUE },
469 469 { ".data.", ".data", B_FALSE },
470 470 { ".dtors.", ".dtors", B_FALSE },
471 471 { ".fini_array.", ".fini_array", B_FALSE },
472 472 { ".init_array.", ".init_array", B_FALSE },
473 473 { ".lbss.", ".lbss", B_FALSE },
474 474 { ".ldata.", ".ldata", B_FALSE },
475 475 { ".lrodata.", ".lrodata", B_FALSE },
476 476 /* This intentionally applies to .rodata.cstN and .rodata.strN, too */
477 477 { ".rodata.", ".rodata", B_FALSE },
478 478 { ".sbss2.", ".sbss2", B_FALSE },
479 479 { ".sbss.", ".sbss", B_FALSE },
480 480 { ".sdata2.", ".sdata2", B_FALSE },
481 481 { ".sdata.", ".sdata", B_FALSE },
482 482 { ".tbss.", ".tbss", B_FALSE },
483 483 { ".tdata.", ".tdata", B_FALSE },
484 484 { ".text.", ".text", B_FALSE },
485 485 { NULL, NULL, B_FALSE }
486 486 };
487 487
488 488 static const char *
489 489 gnu_split_sec(const char *ostr)
490 490 {
491 491 struct split_sec_mapping *mp;
492 492
493 493 for (mp = split_sec_mapping; mp->leader != NULL; mp++) {
494 494 if (mp->precise) {
495 495 if (strcmp(ostr, mp->leader) == 0)
496 496 return (mp->section);
497 497 } else if (strncmp(ostr, mp->leader, strlen(mp->leader)) == 0) {
498 498 return (mp->section);
499 499 }
500 500 }
501 501
502 502 return (ostr);
503 503 }
504 504
505 505 /*
506 506 * Initialize a path info buffer for use with ld_place_section().
507 507 *
508 508 * entry:
509 509 * ofl - Output descriptor
510 510 * ifl - Descriptor for input file, or NULL if there is none.
511 511 * info - Address of buffer to be initialized.
512 512 *
513 513 * exit:
514 514 * If this is an input file, and if the entrance criteria list
515 515 * contains at least one criteria that has a non-empty file string
516 516 * match list (ec_files), then the block pointed at by info is
517 517 * initialized, and info is returned.
518 518 *
519 519 * If there is no input file, and/or no entrance criteria containing
520 520 * a non-empty ec_files list, then NULL is returned. This is not
521 521 * an error --- the NULL is simply an optimization, understood by
522 522 * ld_place_path(), that allows it to skip unnecessary work.
523 523 */
524 524 Place_path_info *
525 525 ld_place_path_info_init(Ofl_desc *ofl, Ifl_desc *ifl, Place_path_info *info)
526 526 {
527 527 /*
528 528 * Return NULL if there is no input file (internally generated section)
529 529 * or if the entrance criteria list does not contain any items that will
530 530 * need to be compared to the path (all the ec_files lists are empty).
531 531 */
532 532 if ((ifl == NULL) || !(ofl->ofl_flags & FLG_OF_EC_FILES))
533 533 return (NULL);
534 534
535 535 info->ppi_path = ifl->ifl_name;
536 536 info->ppi_path_len = strlen(info->ppi_path);
537 537 info->ppi_isar = (ifl->ifl_flags & FLG_IF_EXTRACT) != 0;
538 538
539 539 /*
540 540 * The basename is the final segment of the path, equivalent to
541 541 * the path itself if there are no '/' delimiters.
542 542 */
543 543 info->ppi_bname = strrchr(info->ppi_path, '/');
544 544 if (info->ppi_bname == NULL)
545 545 info->ppi_bname = info->ppi_path;
546 546 else
547 547 info->ppi_bname++; /* Skip leading '/' */
548 548 info->ppi_bname_len =
549 549 info->ppi_path_len - (info->ppi_bname - info->ppi_path);
550 550
551 551 /*
552 552 * For an archive, the object name is the member name, which is
553 553 * enclosed in () at the end of the name string. Otherwise, it is
554 554 * the same as the basename.
555 555 */
556 556 if (info->ppi_isar) {
557 557 info->ppi_oname = strrchr(info->ppi_bname, '(');
558 558 /* There must be an archive member suffix delimited by parens */
559 559 assert((info->ppi_bname[info->ppi_bname_len - 1] == ')') &&
560 560 (info->ppi_oname != NULL));
561 561 info->ppi_oname++; /* skip leading '(' */
562 562 info->ppi_oname_len = info->ppi_bname_len -
563 563 (info->ppi_oname - info->ppi_bname + 1);
564 564 } else {
565 565 info->ppi_oname = info->ppi_bname;
566 566 info->ppi_oname_len = info->ppi_bname_len;
567 567 }
568 568
569 569 return (info);
570 570 }
571 571
572 572 /*
573 573 * Compare an input section path to the file comparison list the given
574 574 * entrance criteria.
575 575 *
576 576 * entry:
577 577 * path_info - A non-NULL Place_path_info block for the file
578 578 * containing the input section, initialized by
579 579 * ld_place_path_info_init()
580 580 * enp - Entrance criteria with a non-empty ec_files list of file
581 581 * comparisons to be carried out.
582 582 *
583 583 * exit:
584 584 * Return TRUE if a match is seen, and FALSE otherwise.
585 585 */
586 586 static Boolean
587 587 eval_ec_files(Place_path_info *path_info, Ent_desc *enp)
588 588 {
589 589 Aliste idx;
590 590 Ent_desc_file *edfp;
591 591 size_t cmp_len;
592 592 const char *cmp_str;
593 593
594 594 for (ALIST_TRAVERSE(enp->ec_files, idx, edfp)) {
595 595 Word type = edfp->edf_flags & TYP_ECF_MASK;
596 596
597 597 /*
598 598 * Determine the starting character, and # of characters,
599 599 * from the file path to compare against this entrance criteria
600 600 * file string.
601 601 */
602 602 if (type == TYP_ECF_OBJNAME) {
603 603 cmp_str = path_info->ppi_oname;
604 604 cmp_len = path_info->ppi_oname_len;
605 605 } else {
606 606 int ar_stat_diff = path_info->ppi_isar !=
607 607 ((edfp->edf_flags & FLG_ECF_ARMEMBER) != 0);
608 608
609 609 /*
610 610 * If the entrance criteria specifies an archive member
611 611 * and the file does not, then there can be no match.
612 612 */
613 613
614 614 if (ar_stat_diff && !path_info->ppi_isar)
615 615 continue;
616 616
617 617 if (type == TYP_ECF_PATH) {
618 618 cmp_str = path_info->ppi_path;
619 619 cmp_len = path_info->ppi_path_len;
620 620 } else { /* TYP_ECF_BASENAME */
621 621 cmp_str = path_info->ppi_bname;
622 622 cmp_len = path_info->ppi_bname_len;
623 623 }
624 624
625 625 /*
626 626 * If the entrance criteria does not specify an archive
627 627 * member and the file does, then a match just requires
628 628 * the paths (without the archive member) to match.
629 629 * Reduce the length to not include the ar member or
630 630 * the '(' that precedes it.
631 631 */
632 632 if (ar_stat_diff && path_info->ppi_isar)
633 633 cmp_len = path_info->ppi_oname - cmp_str - 1;
634 634 }
635 635
636 636 /*
637 637 * Compare the resulting string to the one from the
638 638 * entrance criteria.
639 639 */
640 640 if ((cmp_len == edfp->edf_name_len) &&
641 641 (strncmp(edfp->edf_name, cmp_str, cmp_len) == 0))
642 642 return (TRUE);
643 643 }
644 644
645 645 return (FALSE);
646 646 }
647 647
648 648 /*
649 649 * Replace the section header for the given input section with a new section
650 650 * header of the specified type. All values in the replacement header other
651 651 * than the type retain their previous values.
652 652 *
653 653 * entry:
654 654 * isp - Input section to replace
655 655 * sh_type - New section type to apply
656 656 *
657 657 * exit:
658 658 * Returns the pointer to the new section header on success, and
659 659 * NULL for failure.
660 660 */
661 661 static Shdr *
662 662 isp_convert_type(Is_desc *isp, Word sh_type)
663 663 {
664 664 Shdr *shdr;
665 665
666 666 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
667 667 return (NULL);
668 668 *shdr = *isp->is_shdr;
669 669 isp->is_shdr = shdr;
670 670 shdr->sh_type = sh_type;
671 671 return (shdr);
672 672 }
673 673
674 674 /*
675 675 * Issue a fatal warning for the given .eh_frame section, which
676 676 * cannot be merged with the existing .eh_frame output section.
677 677 */
678 678 static void
679 679 eh_frame_muldef(Ofl_desc *ofl, Is_desc *isp)
680 680 {
681 681 Sg_desc *sgp;
682 682 Is_desc *isp1;
683 683 Os_desc *osp;
684 684 Aliste idx1, idx2, idx3;
685 685
686 686 /*
687 687 * Locate the .eh_frame output section, and use the first section
688 688 * assigned to it in the error message. The user can then compare
689 689 * the two sections to determine what attribute prevented the merge.
690 690 */
691 691 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
692 692 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
693 693 if ((osp->os_flags & FLG_OS_EHFRAME) == 0)
694 694 continue;
695 695
696 696 for (idx3 = 0; idx3 < OS_ISD_NUM; idx3++) {
697 697 APlist *lst = osp->os_isdescs[idx3];
698 698
699 699 if (aplist_nitems(lst) == 0)
700 700 continue;
701 701
702 702 isp1 = lst->apl_data[0];
703 703 ld_eprintf(ofl, ERR_FATAL,
704 704 MSG_INTL(MSG_UPD_MULEHFRAME),
705 705 isp1->is_file->ifl_name,
706 706 EC_WORD(isp1->is_scnndx), isp1->is_name,
707 707 isp->is_file->ifl_name,
708 708 EC_WORD(isp->is_scnndx), isp->is_name);
709 709 return;
710 710 }
711 711 }
712 712 }
713 713 }
714 714
715 715 /*
716 716 * Place a section into the appropriate segment and output section.
717 717 *
718 718 * entry:
719 719 * ofl - File descriptor
720 720 * isp - Input section descriptor of section to be placed.
721 721 * path_info - NULL, or pointer to Place_path_info buffer initialized
722 722 * by ld_place_path_info_init() for the file associated to isp,
723 723 * for use in processing entrance criteria with non-empty
724 724 * file matching string list (ec_files)
725 725 * ident - Section identifier, used to order sections relative to
726 726 * others within the output segment.
727 727 * alt_os_name - If non-NULL, the name of the output section to place
728 728 * isp into. If NULL, input sections go to an output section
729 729 * with the same name as the input section.
730 730 */
731 731 Os_desc *
732 732 ld_place_section(Ofl_desc *ofl, Is_desc *isp, Place_path_info *path_info,
733 733 int ident, const char *alt_os_name)
734 734 {
735 735 Ent_desc *enp;
736 736 Sg_desc *sgp;
737 737 Os_desc *osp;
738 738 Aliste idx1, iidx;
739 739 int os_ndx;
740 740 Shdr *shdr = isp->is_shdr;
741 741 Xword shflagmask, shflags = shdr->sh_flags;
742 742 Ifl_desc *ifl = isp->is_file;
743 743 char *oname, *sname;
744 744 uint_t onamehash;
745 745 Boolean is_ehframe = (isp->is_flags & FLG_IS_EHFRAME) != 0;
746 746
747 747 /*
748 748 * Define any sections that must be thought of as referenced. These
749 749 * sections may not be referenced externally in a manner ld(1) can
750 750 * discover, but they must be retained (ie. not removed by -zignore).
751 751 */
↓ open down ↓ |
751 lines elided |
↑ open up ↑ |
752 752 static const Msg RefSecs[] = {
753 753 MSG_SCN_INIT, /* MSG_ORIG(MSG_SCN_INIT) */
754 754 MSG_SCN_FINI, /* MSG_ORIG(MSG_SCN_FINI) */
755 755 MSG_SCN_EX_RANGES, /* MSG_ORIG(MSG_SCN_EX_RANGES) */
756 756 MSG_SCN_EX_SHARED, /* MSG_ORIG(MSG_SCN_EX_SHARED) */
757 757 MSG_SCN_CTORS, /* MSG_ORIG(MSG_SCN_CTORS) */
758 758 MSG_SCN_DTORS, /* MSG_ORIG(MSG_SCN_DTORS) */
759 759 MSG_SCN_EHFRAME, /* MSG_ORIG(MSG_SCN_EHFRAME) */
760 760 MSG_SCN_EHFRAME_HDR, /* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */
761 761 MSG_SCN_JCR, /* MSG_ORIG(MSG_SCN_JCR) */
762 + MSG_SCN_INITARRAY, /* MSG_ORIG(MSG_SCN_INITARRAY) */
763 + MSG_SCN_FINIARRAY, /* MSG_ORIG(MSG_SCN_FINIARRAY) */
764 + MSG_SCN_PREINITARRAY, /* MSG_ORIG(MSG_SCN_PREINITARRAY) */
762 765 0
763 766 };
764 767
765 768 DBG_CALL(Dbg_sec_in(ofl->ofl_lml, isp));
766 769
767 770 /*
768 771 * If this section identifies group members, or this section indicates
769 772 * that it is a member of a group, determine whether the section is
770 773 * still required.
771 774 */
772 775 if ((shflags & SHF_GROUP) || (shdr->sh_type == SHT_GROUP)) {
773 776 Group_desc *gdesc;
774 777
775 778 if ((gdesc = ld_get_group(ofl, isp)) != NULL) {
776 779 DBG_CALL(Dbg_sec_group(ofl->ofl_lml, isp, gdesc));
777 780
778 781 /*
779 782 * If this group has been replaced by another group,
780 783 * then this section needs to be discarded.
781 784 */
782 785 if (gdesc->gd_oisc) {
783 786 isp->is_flags |= FLG_IS_DISCARD;
784 787
785 788 /*
786 789 * Since we're discarding the section, we
787 790 * can skip assigning it to an output section.
788 791 * The exception is that if the user
789 792 * specifies -z relaxreloc, then
790 793 * we need to assign the output section so
791 794 * that the sloppy relocation logic will have
792 795 * the information necessary to do its work.
793 796 */
794 797 if (!(ofl->ofl_flags1 & FLG_OF1_RLXREL))
795 798 return (NULL);
796 799 }
797 800 }
798 801
799 802 /*
800 803 * SHT_GROUP sections can only be included into relocatable
801 804 * objects.
802 805 */
803 806 if (shdr->sh_type == SHT_GROUP) {
804 807 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
805 808 isp->is_flags |= FLG_IS_DISCARD;
806 809 return (NULL);
807 810 }
808 811 }
809 812 }
810 813
811 814 /*
812 815 * Always assign SHF_TLS sections to the DATA segment (and then the
813 816 * PT_TLS embedded inside of there).
814 817 */
815 818 if (shflags & SHF_TLS)
816 819 shflags |= SHF_WRITE;
817 820
818 821 /*
819 822 * Traverse the entrance criteria list searching for a segment that
820 823 * matches the input section we have. If an entrance criterion is set
821 824 * then there must be an exact match. If we complete the loop without
822 825 * finding a segment, then sgp will be NULL.
823 826 */
824 827 sgp = NULL;
825 828 for (APLIST_TRAVERSE(ofl->ofl_ents, idx1, enp)) {
826 829
827 830 /* Disabled segments are not available for assignment */
828 831 if (enp->ec_segment->sg_flags & FLG_SG_DISABLED)
829 832 continue;
830 833
831 834 /*
832 835 * If an entrance criteria doesn't have any of its fields
833 836 * set, it will match any section it is tested against.
834 837 * We set the FLG_EC_CATCHALL flag on these, primarily because
835 838 * it helps readers of our debug output to understand what
836 839 * the criteria means --- otherwise the user would just see
837 840 * that every field is 0, but might not understand the
838 841 * significance of that.
839 842 *
840 843 * Given that we set this flag, we can use it here as an
841 844 * optimization to short circuit all of the tests in this
842 845 * loop. Note however, that if we did not do this, the end
843 846 * result would be the same --- the empty criteria will sail
844 847 * past the following tests and reach the end of the loop.
845 848 */
846 849 if (enp->ec_flags & FLG_EC_CATCHALL) {
847 850 sgp = enp->ec_segment;
848 851 break;
849 852 }
850 853
851 854 if (enp->ec_type && (enp->ec_type != shdr->sh_type))
852 855 continue;
853 856 if (enp->ec_attrmask &&
854 857 /* LINTED */
855 858 (enp->ec_attrmask & enp->ec_attrbits) !=
856 859 (enp->ec_attrmask & shflags))
857 860 continue;
858 861 if (enp->ec_is_name &&
859 862 (strcmp(enp->ec_is_name, isp->is_name) != 0))
860 863 continue;
861 864
862 865 if ((alist_nitems(enp->ec_files) > 0) &&
863 866 ((path_info == NULL) || !eval_ec_files(path_info, enp)))
864 867 continue;
865 868
866 869 /* All entrance criteria tests passed */
867 870 sgp = enp->ec_segment;
868 871 break;
869 872 }
870 873
871 874 /*
872 875 * The final entrance criteria record is a FLG_EC_CATCHALL that points
873 876 * at the final predefined segment "extra", and this final segment is
874 877 * tagged FLG_SG_NODISABLE. Therefore, the above loop must always find
875 878 * a segment.
876 879 */
877 880 assert(sgp != NULL);
878 881
879 882 /*
880 883 * Transfer the input section sorting key from the entrance criteria
881 884 * to the input section. A non-zero value means that the section
882 885 * will be sorted on this key amoung the other sections that have a
883 886 * non-zero key. These sorted sections are collectively placed at the
884 887 * head of the output section.
885 888 *
886 889 * If the sort key is 0, the section is placed after the sorted
887 890 * sections in the order they are encountered.
888 891 */
889 892 isp->is_ordndx = enp->ec_ordndx;
890 893
891 894 /* Remember that this entrance criteria has placed a section */
892 895 enp->ec_flags |= FLG_EC_USED;
893 896
894 897 /*
895 898 * If our caller has supplied an alternative name for the output
896 899 * section, then we defer to their request. Otherwise, the default
897 900 * is to use the same name as that of the input section being placed.
898 901 *
899 902 * The COMDAT, SHT_GROUP and GNU name translations that follow have
900 903 * the potential to alter this initial name.
901 904 */
902 905 oname = (char *)((alt_os_name == NULL) ? isp->is_name : alt_os_name);
903 906
904 907 /*
905 908 * Solaris section names may follow the convention:
906 909 *
907 910 * section-name%symbol-name
908 911 *
909 912 * This convention has been used to order the layout of sections within
910 913 * segments for objects built with the compilers -xF option. However,
911 914 * the final object should not contain individual section headers for
912 915 * all such input sections, instead the symbol name is stripped from the
913 916 * name to establish the final output section name.
914 917 *
915 918 * This convention has also been followed for COMDAT and sections
916 919 * identified though SHT_GROUP data.
917 920 *
918 921 * Strip out the % from the section name for:
919 922 * - Non-relocatable objects
920 923 * - Relocatable objects if input section sorting is
921 924 * in force for the segment in question.
922 925 */
923 926 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) ||
924 927 (sgp->sg_flags & FLG_SG_IS_ORDER)) {
925 928 if ((sname = strchr(isp->is_name, '%')) != NULL) {
926 929 size_t size = sname - isp->is_name;
927 930
928 931 if ((oname = libld_malloc(size + 1)) == NULL)
929 932 return ((Os_desc *)S_ERROR);
930 933 (void) strncpy(oname, isp->is_name, size);
931 934 oname[size] = '\0';
932 935 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
933 936 }
934 937 }
935 938
936 939 /*
937 940 * When building relocatable objects, we must not redirect COMDAT
938 941 * section names into their outputs, such that our output object may
939 942 * be successfully used as an input object also requiring COMDAT
940 943 * processing
941 944 */
942 945
943 946 /*
944 947 * GNU section names may follow the convention:
945 948 *
946 949 * .gnu.linkonce.*
947 950 *
948 951 * The .gnu.linkonce is a section naming convention that indicates a
949 952 * COMDAT requirement. Determine whether this section follows the GNU
950 953 * pattern, and if so, determine whether this section should be
951 954 * discarded or retained. The comparison of is_name[1] with 'g'
952 955 * is an optimization to skip using strncmp() too much. This is safe,
953 956 * because we know the name is not NULL, and therefore must have
954 957 * at least one character plus a NULL termination.
955 958 */
956 959 if ((isp->is_name == oname) && (isp->is_name[1] == 'g') &&
957 960 (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
958 961 MSG_SCN_GNU_LINKONCE_SIZE) == 0)) {
959 962 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
960 963 if ((oname = (char *)gnu_linkonce_sec(isp->is_name)) !=
961 964 isp->is_name) {
962 965 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp,
963 966 oname));
964 967 }
965 968 }
966 969
967 970 /*
968 971 * Explicitly identify this section type as COMDAT. Also,
969 972 * enable relaxed relocation processing, as this is typically
970 973 * a requirement with .gnu.linkonce sections.
971 974 */
972 975 isp->is_flags |= FLG_IS_COMDAT;
973 976 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
974 977 ofl->ofl_flags1 |= FLG_OF1_RLXREL;
975 978 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, TRUE,
976 979 (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0));
977 980 }
978 981
979 982 /*
980 983 * GNU section names may also follow the convention:
981 984 *
982 985 * section-name.symbol-name
983 986 *
984 987 * This convention is used when defining SHT_GROUP sections of type
985 988 * COMDAT. Thus, any group processing will have discovered any group
986 989 * sections, and this identification can be triggered by a pattern
987 990 * match section names.
988 991 */
989 992 if ((isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) &&
990 993 ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) {
991 994 size_t size = sname - isp->is_name;
992 995
993 996 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
994 997 if ((oname = libld_malloc(size + 1)) == NULL)
995 998 return ((Os_desc *)S_ERROR);
996 999 (void) strncpy(oname, isp->is_name, size);
997 1000 oname[size] = '\0';
998 1001 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
999 1002 }
1000 1003
1001 1004 /*
1002 1005 * Enable relaxed relocation processing, as this is
1003 1006 * typically a requirement with GNU COMDAT sections.
1004 1007 */
1005 1008 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) {
1006 1009 ofl->ofl_flags1 |= FLG_OF1_RLXREL;
1007 1010 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp,
1008 1011 FALSE, TRUE));
1009 1012 }
1010 1013 }
1011 1014
1012 1015 /*
1013 1016 * GNU section names named section-name.symbol-name which are not
1014 1017 * members of COMDAT groups are merged according to the behaviour of
1015 1018 * the GNU link-editor.
1016 1019 *
1017 1020 * See the description of gnu_split_sec().
1018 1021 */
1019 1022 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
1020 1023 (isp->is_name == oname) &&
1021 1024 ((oname = (char *)gnu_split_sec(oname)) != isp->is_name)) {
1022 1025 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
1023 1026 }
1024 1027
1025 1028 /*
1026 1029 * Assign a hash value now that the output section name has been
1027 1030 * finalized.
1028 1031 */
1029 1032 onamehash = sgs_str_hash(oname);
1030 1033
1031 1034 /*
1032 1035 * Determine if output section ordering is turned on. If so, return
1033 1036 * the appropriate ordering index for the section. This information
1034 1037 * is derived from the Sg_desc->sg_os_order list that was built
1035 1038 * up from the Mapfile.
1036 1039 *
1037 1040 * A value of 0 for os_ndx means that the section is not sorted
1038 1041 * (i.e. is not found in the sg_os_order). The items in sg_os_order
1039 1042 * are in the desired sort order, so adding 1 to their alist index
1040 1043 * gives a suitable index for sorting.
1041 1044 */
1042 1045 os_ndx = 0;
1043 1046 if (alist_nitems(sgp->sg_os_order) > 0) {
1044 1047 Sec_order *scop;
1045 1048
1046 1049 for (ALIST_TRAVERSE(sgp->sg_os_order, idx1, scop)) {
1047 1050 if (strcmp(scop->sco_secname, oname) == 0) {
1048 1051 scop->sco_flags |= FLG_SGO_USED;
1049 1052 os_ndx = idx1 + 1;
1050 1053 break;
1051 1054 }
1052 1055 }
1053 1056 }
1054 1057
1055 1058 /*
1056 1059 * Mask of section header flags to ignore when matching sections. We
1057 1060 * are more strict with relocatable objects, ignoring only the order
1058 1061 * flags, and keeping sections apart if they differ otherwise. This
1059 1062 * follows the policy that sections in a relative object should only
1060 1063 * be merged if their flags are the same, and avoids destroying
1061 1064 * information prematurely. For final products however, we ignore all
1062 1065 * flags that do not prevent a merge.
1063 1066 */
1064 1067 shflagmask =
1065 1068 (ofl->ofl_flags & FLG_OF_RELOBJ) ? ALL_SHF_ORDER : ALL_SHF_IGNORE;
1066 1069
1067 1070 /*
1068 1071 * Traverse the input section list for the output section we have been
1069 1072 * assigned. If we find a matching section simply add this new section.
1070 1073 */
1071 1074 iidx = 0;
1072 1075 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) {
1073 1076 Shdr *os_shdr = osp->os_shdr;
1074 1077
1075 1078 /*
1076 1079 * An input section matches an output section if:
1077 1080 * - The ident values match
1078 1081 * - The names match
1079 1082 * - Not a GROUP section
1080 1083 * - Not a DTrace dof section
1081 1084 * - Section types match
1082 1085 * - Matching section flags, after screening out the
1083 1086 * shflagmask flags.
1084 1087 *
1085 1088 * Section types are considered to match if any one of
1086 1089 * the following are true:
1087 1090 * - The type codes are the same
1088 1091 * - Both are .eh_frame sections (regardless of type code)
1089 1092 * - The input section is COMDAT, and the output section
1090 1093 * is SHT_PROGBITS.
1091 1094 */
1092 1095 if ((ident == osp->os_identndx) &&
1093 1096 (ident != ld_targ.t_id.id_rel) &&
1094 1097 (onamehash == osp->os_namehash) &&
1095 1098 (shdr->sh_type != SHT_GROUP) &&
1096 1099 (shdr->sh_type != SHT_SUNW_dof) &&
1097 1100 ((shdr->sh_type == os_shdr->sh_type) ||
1098 1101 (is_ehframe && (osp->os_flags & FLG_OS_EHFRAME)) ||
1099 1102 ((shdr->sh_type == SHT_SUNW_COMDAT) &&
1100 1103 (os_shdr->sh_type == SHT_PROGBITS))) &&
1101 1104 ((shflags & ~shflagmask) ==
1102 1105 (os_shdr->sh_flags & ~shflagmask)) &&
1103 1106 (strcmp(oname, osp->os_name) == 0)) {
1104 1107 uintptr_t err;
1105 1108
1106 1109 /*
1107 1110 * Process any COMDAT section, keeping the first and
1108 1111 * discarding all others.
1109 1112 */
1110 1113 if ((isp->is_flags & FLG_IS_COMDAT) &&
1111 1114 ((err = add_comdat(ofl, osp, isp)) != 1))
1112 1115 return ((Os_desc *)err);
1113 1116
1114 1117 /*
1115 1118 * Set alignment
1116 1119 */
1117 1120 set_addralign(ofl, osp, isp);
1118 1121
1119 1122 /*
1120 1123 * If this section is a non-empty TLS section indicate
1121 1124 * that a PT_TLS program header is required.
1122 1125 */
1123 1126 if ((shflags & SHF_TLS) && shdr->sh_size &&
1124 1127 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
1125 1128 ofl->ofl_flags |= FLG_OF_TLSPHDR;
1126 1129
1127 1130 /*
1128 1131 * Insert the input section descriptor on the proper
1129 1132 * output section descriptor list.
1130 1133 *
1131 1134 * If this segment requires input section ordering,
1132 1135 * honor any mapfile specified ordering for otherwise
1133 1136 * unordered sections by setting the mapfile_sort
1134 1137 * argument of os_attach_isp() to True.
1135 1138 */
1136 1139
1137 1140 if (os_attach_isp(ofl, osp, isp,
1138 1141 (sgp->sg_flags & FLG_SG_IS_ORDER) != 0) == 0)
1139 1142 return ((Os_desc *)S_ERROR);
1140 1143
1141 1144 /*
1142 1145 * If this input section and file is associated to an
1143 1146 * artificially referenced output section, make sure
1144 1147 * they are marked as referenced also. This ensures
1145 1148 * that this input section and file isn't eliminated
1146 1149 * when -zignore is in effect.
1147 1150 *
1148 1151 * See -zignore comments when creating a new output
1149 1152 * section below.
1150 1153 */
1151 1154 if (((ifl &&
1152 1155 (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) &&
1153 1156 (osp->os_flags & FLG_OS_SECTREF)) {
1154 1157 isp->is_flags |= FLG_IS_SECTREF;
1155 1158 if (ifl)
1156 1159 ifl->ifl_flags |= FLG_IF_FILEREF;
1157 1160 }
1158 1161
1159 1162 DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
1160 1163 return (osp);
1161 1164 }
1162 1165
1163 1166 /*
1164 1167 * Do we need to worry about section ordering?
1165 1168 */
1166 1169 if (os_ndx) {
1167 1170 if (osp->os_ordndx) {
1168 1171 if (os_ndx < osp->os_ordndx)
1169 1172 /* insert section here. */
1170 1173 break;
1171 1174 else {
1172 1175 iidx = idx1 + 1;
1173 1176 continue;
1174 1177 }
1175 1178 } else {
1176 1179 /* insert section here. */
1177 1180 break;
1178 1181 }
1179 1182 } else if (osp->os_ordndx) {
1180 1183 iidx = idx1 + 1;
1181 1184 continue;
1182 1185 }
1183 1186
1184 1187 /*
1185 1188 * If the new sections identifier is less than that of the
1186 1189 * present input section we need to insert the new section
1187 1190 * at this point.
1188 1191 */
1189 1192 if (ident < osp->os_identndx)
1190 1193 break;
1191 1194
1192 1195 iidx = idx1 + 1;
1193 1196 }
1194 1197
1195 1198 /*
1196 1199 * We are adding a new output section. Update the section header
1197 1200 * count and associated string size.
1198 1201 *
1199 1202 * If the input section triggering this output section has been marked
1200 1203 * for discard, and if no other non-discarded input section comes along
1201 1204 * to join it, then we will over count. We cannot know if this will
1202 1205 * happen or not until all input is seen. Set FLG_OF_AJDOSCNT to
1203 1206 * trigger a final count readjustment.
1204 1207 */
1205 1208 if (isp->is_flags & FLG_IS_DISCARD)
1206 1209 ofl->ofl_flags |= FLG_OF_ADJOSCNT;
1207 1210 ofl->ofl_shdrcnt++;
1208 1211 if (st_insert(ofl->ofl_shdrsttab, oname) == -1)
1209 1212 return ((Os_desc *)S_ERROR);
1210 1213
1211 1214 /*
1212 1215 * Create a new output section descriptor.
1213 1216 */
1214 1217 if ((osp = libld_calloc(sizeof (Os_desc), 1)) == NULL)
1215 1218 return ((Os_desc *)S_ERROR);
1216 1219 if ((osp->os_shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
1217 1220 return ((Os_desc *)S_ERROR);
1218 1221
1219 1222 /*
1220 1223 * Convert COMDAT section to PROGBITS as this the first section of the
1221 1224 * output section. Save any COMDAT section for later processing, as
1222 1225 * additional COMDAT sections that match this section need discarding.
1223 1226 */
1224 1227 if ((shdr->sh_type == SHT_SUNW_COMDAT) &&
1225 1228 ((shdr = isp_convert_type(isp, SHT_PROGBITS)) == NULL))
1226 1229 return ((Os_desc *)S_ERROR);
1227 1230 if ((isp->is_flags & FLG_IS_COMDAT) &&
1228 1231 (add_comdat(ofl, osp, isp) == S_ERROR))
1229 1232 return ((Os_desc *)S_ERROR);
1230 1233
1231 1234 if (is_ehframe) {
1232 1235 /*
1233 1236 * Executable or sharable objects can have at most a single
1234 1237 * .eh_frame section. Detect attempts to create more than
1235 1238 * one. This occurs if the input sections have incompatible
1236 1239 * attributes.
1237 1240 */
1238 1241 if ((ofl->ofl_flags & FLG_OF_EHFRAME) &&
1239 1242 !(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1240 1243 eh_frame_muldef(ofl, isp);
1241 1244 return ((Os_desc *)S_ERROR);
1242 1245 }
1243 1246 ofl->ofl_flags |= FLG_OF_EHFRAME;
1244 1247
1245 1248 /*
1246 1249 * For .eh_frame sections, we always set the type to be the
1247 1250 * type specified by the ABI. This allows .eh_frame sections
1248 1251 * of type SHT_PROGBITS to be correctly merged with .eh_frame
1249 1252 * sections of the ABI-defined type (e.g. SHT_AMD64_UNWIND),
1250 1253 * with the output being of the ABI-defined type.
1251 1254 */
1252 1255 osp->os_shdr->sh_type = ld_targ.t_m.m_sht_unwind;
1253 1256 } else {
1254 1257 osp->os_shdr->sh_type = shdr->sh_type;
1255 1258 }
1256 1259
1257 1260 osp->os_shdr->sh_flags = shdr->sh_flags;
1258 1261 osp->os_shdr->sh_entsize = shdr->sh_entsize;
1259 1262 osp->os_name = oname;
1260 1263 osp->os_namehash = onamehash;
1261 1264 osp->os_ordndx = os_ndx;
1262 1265 osp->os_sgdesc = sgp;
1263 1266 if (is_ehframe)
1264 1267 osp->os_flags |= FLG_OS_EHFRAME;
1265 1268
1266 1269 if (ifl && (shdr->sh_type == SHT_PROGBITS)) {
1267 1270 /*
1268 1271 * Try to preserve the intended meaning of sh_link/sh_info.
1269 1272 * See the translate_link() in update.c.
1270 1273 */
1271 1274 osp->os_shdr->sh_link = shdr->sh_link;
1272 1275 if (shdr->sh_flags & SHF_INFO_LINK)
1273 1276 osp->os_shdr->sh_info = shdr->sh_info;
1274 1277 }
1275 1278
1276 1279 /*
1277 1280 * When -zignore is in effect, user supplied sections and files that are
1278 1281 * not referenced from other sections, are eliminated from the object
1279 1282 * being produced. Some sections, although unreferenced, are special,
1280 1283 * and must not be eliminated. Determine if this new output section is
1281 1284 * one of those special sections, and if so mark it artificially as
1282 1285 * referenced. Any input section and file associated to this output
1283 1286 * section is also be marked as referenced, and thus won't be eliminated
1284 1287 * from the final output.
1285 1288 */
1286 1289 if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) {
1287 1290 const Msg *refsec;
1288 1291
1289 1292 for (refsec = RefSecs; *refsec; refsec++) {
1290 1293 if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) {
1291 1294 osp->os_flags |= FLG_OS_SECTREF;
1292 1295
1293 1296 if ((ifl->ifl_flags & FLG_IF_IGNORE) ||
1294 1297 DBG_ENABLED) {
1295 1298 isp->is_flags |= FLG_IS_SECTREF;
1296 1299 ifl->ifl_flags |= FLG_IF_FILEREF;
1297 1300 }
1298 1301 break;
1299 1302 }
1300 1303 }
1301 1304 }
1302 1305
1303 1306 /*
1304 1307 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list,
1305 1308 * so that they can be updated as a group later.
1306 1309 */
1307 1310 if ((shdr->sh_type == SHT_GROUP) &&
1308 1311 ((isp->is_flags & FLG_IS_DISCARD) == 0) &&
1309 1312 (aplist_append(&ofl->ofl_osgroups, osp,
1310 1313 AL_CNT_OFL_OSGROUPS) == NULL))
1311 1314 return ((Os_desc *)S_ERROR);
1312 1315
1313 1316 /*
1314 1317 * If this section is a non-empty TLS section indicate that a PT_TLS
1315 1318 * program header is required.
1316 1319 */
1317 1320 if ((shflags & SHF_TLS) && shdr->sh_size &&
1318 1321 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
1319 1322 ofl->ofl_flags |= FLG_OF_TLSPHDR;
1320 1323
1321 1324 /*
1322 1325 * If a non-allocatable section is going to be put into a loadable
1323 1326 * segment then turn on the allocate bit for this section and warn the
1324 1327 * user that we have done so. This could only happen through the use
1325 1328 * of a mapfile.
1326 1329 */
1327 1330 if ((sgp->sg_phdr.p_type == PT_LOAD) &&
1328 1331 ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) {
1329 1332 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC),
1330 1333 ofl->ofl_name, osp->os_name, sgp->sg_name);
1331 1334 osp->os_shdr->sh_flags |= SHF_ALLOC;
1332 1335 }
1333 1336
1334 1337 /*
1335 1338 * Retain this sections identifier for future comparisons when placing
1336 1339 * a section (after all sections have been processed this variable will
1337 1340 * be used to hold the sections symbol index as we don't need to retain
1338 1341 * the identifier any more).
1339 1342 */
1340 1343 osp->os_identndx = ident;
1341 1344
1342 1345 /*
1343 1346 * Set alignment.
1344 1347 */
1345 1348 set_addralign(ofl, osp, isp);
1346 1349
1347 1350 if (os_attach_isp(ofl, osp, isp, 0) == 0)
1348 1351 return ((Os_desc *)S_ERROR);
1349 1352
1350 1353 DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp));
1351 1354
1352 1355 /*
1353 1356 * Insert the new section at the offset given by iidx. If no position
1354 1357 * for it was identified above, this will be index 0, causing the new
1355 1358 * section to be prepended to the beginning of the section list.
1356 1359 * Otherwise, it is the index following the section that was identified.
1357 1360 */
1358 1361 if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC,
1359 1362 iidx) == NULL)
1360 1363 return ((Os_desc *)S_ERROR);
1361 1364 return (osp);
1362 1365 }
↓ open down ↓ |
591 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX