Print this page
3453 GNU comdat redirection does exactly the wrong thing
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
242 242 /*
243 243 * Create a COMDAT avl tree for this output section if required.
244 244 */
245 245 if ((avlt = osp->os_comdats) == NULL) {
246 246 if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
247 247 return (S_ERROR);
248 248 avl_create(avlt, isdavl_compare, sizeof (Isd_node),
249 249 SGSOFFSETOF(Isd_node, isd_avl));
250 250 osp->os_comdats = avlt;
251 251 }
252 252
253 253 /*
254 254 * A standard COMDAT section uses the section name as search key.
255 255 */
256 256 isd.isd_name = isp->is_name;
257 257 isd.isd_hash = sgs_str_hash(isd.isd_name);
258 258
259 259 if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
260 260 isp->is_osdesc = osp;
261 261
262 262 /*
263 263 * If this section hasn't already been identified as discarded,
264 264 * generate a suitable diagnostic.
265 265 */
266 266 if ((isp->is_flags & FLG_IS_DISCARD) == 0) {
267 267 isp->is_flags |= FLG_IS_DISCARD;
268 268 isp->is_comdatkeep = isdp->isd_isp;
269 269 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp,
270 270 isdp->isd_isp));
271 271 }
272 272
273 273 /*
274 274 * A discarded section does not require assignment to an output
275 275 * section. However, if relaxed relocations have been enabled
276 276 * (either from -z relaxreloc, or asserted with .gnu.linkonce
277 277 * processing), then this section must still be assigned to an
278 278 * output section so that the sloppy relocation logic will have
279 279 * the information necessary to do its work.
280 280 */
281 281 return (0);
282 282 }
283 283
284 284 /*
285 285 * This is a new COMDAT section - so keep it.
286 286 */
287 287 if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL)
288 288 return (S_ERROR);
289 289
290 290 isdp->isd_name = isd.isd_name;
291 291 isdp->isd_hash = isd.isd_hash;
292 292 isdp->isd_isp = isp;
293 293
294 294 avl_insert(avlt, isdp, where);
295 295 return (1);
296 296 }
297 297
298 298 /*
299 299 * Determine whether a GNU group COMDAT section name follows the convention
300 300 *
301 301 * section-name.symbol-name
302 302 *
303 303 * Each section within the input file is compared to see if the full section
304 304 * name matches the beginning of the COMDAT section, with a following '.'.
305 305 * A pointer to the symbol name, starting with the '.' is returned so that the
306 306 * caller can strip off the required section name.
307 307 */
308 308 static char *
309 309 gnu_comdat_sym(Ifl_desc *ifl, Is_desc *gisp)
310 310 {
311 311 size_t ndx;
312 312
313 313 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
314 314 Is_desc *isp;
315 315 size_t ssize;
316 316
317 317 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
↓ open down ↓ |
317 lines elided |
↑ open up ↑ |
318 318 (isp == gisp) || (isp->is_name == NULL))
319 319 continue;
320 320
321 321 /*
322 322 * It's questionable whether this size should be cached in the
323 323 * Is_desc. However, this seems an infrequent operation and
324 324 * adding Is_desc members can escalate memory usage for large
325 325 * link-edits. For now, size the section name dynamically.
326 326 */
327 327 ssize = strlen(isp->is_name);
328 - if ((strncmp(isp->is_name, gisp->is_name, ssize) != 0) &&
328 + if ((strncmp(isp->is_name, gisp->is_name, ssize) == 0) &&
329 329 (gisp->is_name[ssize] == '.'))
330 330 return ((char *)&gisp->is_name[ssize]);
331 331 }
332 332 return (NULL);
333 333 }
334 334
335 335 /*
336 336 * GNU .gnu.linkonce sections follow a naming convention that indicates the
337 337 * required association with an output section. Determine whether this input
338 338 * section follows the convention, and if so return the appropriate output
339 339 * section name.
340 340 *
341 341 * .gnu.linkonce.b.* -> .bss
342 342 * .gnu.linkonce.d.* -> .data
343 343 * .gnu.linkonce.l.* -> .ldata
344 344 * .gnu.linkonce.lb.* -> .lbss
345 345 * .gnu.linkonce.lr.* -> .lrodata
346 346 * .gnu.linkonce.r.* -> .rodata
347 347 * .gnu.linkonce.s.* -> .sdata
348 348 * .gnu.linkonce.s2.* -> .sdata2
349 349 * .gnu.linkonce.sb.* -> .sbss
350 350 * .gnu.linkonce.sb2.* -> .sbss2
351 351 * .gnu.linkonce.t.* -> .text
352 352 * .gnu.linkonce.tb.* -> .tbss
353 353 * .gnu.linkonce.td.* -> .tdata
354 354 * .gnu.linkonce.wi.* -> .debug_info
355 355 */
356 356 #define NSTR_CH1(ch) (*(nstr + 1) == (ch))
357 357 #define NSTR_CH2(ch) (*(nstr + 2) == (ch))
358 358 #define NSTR_CH3(ch) (*(nstr + 3) == (ch))
359 359
360 360 static const char *
361 361 gnu_linkonce_sec(const char *ostr)
362 362 {
363 363 const char *nstr = &ostr[MSG_SCN_GNU_LINKONCE_SIZE];
364 364
365 365 switch (*nstr) {
366 366 case 'b':
367 367 if (NSTR_CH1('.'))
368 368 return (MSG_ORIG(MSG_SCN_BSS));
369 369 break;
370 370 case 'd':
371 371 if (NSTR_CH1('.'))
372 372 return (MSG_ORIG(MSG_SCN_DATA));
373 373 break;
374 374 case 'l':
375 375 if (NSTR_CH1('.'))
376 376 return (MSG_ORIG(MSG_SCN_LDATA));
377 377 else if (NSTR_CH1('b') && NSTR_CH2('.'))
378 378 return (MSG_ORIG(MSG_SCN_LBSS));
379 379 else if (NSTR_CH1('r') && NSTR_CH2('.'))
380 380 return (MSG_ORIG(MSG_SCN_LRODATA));
381 381 break;
382 382 case 'r':
383 383 if (NSTR_CH1('.'))
384 384 return (MSG_ORIG(MSG_SCN_RODATA));
385 385 break;
386 386 case 's':
387 387 if (NSTR_CH1('.'))
388 388 return (MSG_ORIG(MSG_SCN_SDATA));
389 389 else if (NSTR_CH1('2') && NSTR_CH2('.'))
390 390 return (MSG_ORIG(MSG_SCN_SDATA2));
391 391 else if (NSTR_CH1('b') && NSTR_CH2('.'))
392 392 return (MSG_ORIG(MSG_SCN_SBSS));
393 393 else if (NSTR_CH1('b') && NSTR_CH2('2') && NSTR_CH3('.'))
394 394 return (MSG_ORIG(MSG_SCN_SBSS2));
395 395 break;
396 396 case 't':
397 397 if (NSTR_CH1('.'))
398 398 return (MSG_ORIG(MSG_SCN_TEXT));
399 399 else if (NSTR_CH1('b') && NSTR_CH2('.'))
400 400 return (MSG_ORIG(MSG_SCN_TBSS));
401 401 else if (NSTR_CH1('d') && NSTR_CH2('.'))
402 402 return (MSG_ORIG(MSG_SCN_TDATA));
403 403 break;
404 404 case 'w':
405 405 if (NSTR_CH1('i') && NSTR_CH2('.'))
406 406 return (MSG_ORIG(MSG_SCN_DEBUG_INFO));
407 407 break;
408 408 default:
409 409 break;
410 410 }
411 411
412 412 /*
413 413 * No special name match found.
414 414 */
415 415 return (ostr);
416 416 }
417 417 #undef NSTR_CH1
418 418 #undef NSTR_CH2
419 419 #undef NSTR_CH3
420 420
421 421
422 422 /*
423 423 * Initialize a path info buffer for use with ld_place_section().
424 424 *
425 425 * entry:
426 426 * ofl - Output descriptor
427 427 * ifl - Descriptor for input file, or NULL if there is none.
428 428 * info - Address of buffer to be initialized.
429 429 *
430 430 * exit:
431 431 * If this is an input file, and if the entrance criteria list
432 432 * contains at least one criteria that has a non-empty file string
433 433 * match list (ec_files), then the block pointed at by info is
434 434 * initialized, and info is returned.
435 435 *
436 436 * If there is no input file, and/or no entrance criteria containing
437 437 * a non-empty ec_files list, then NULL is returned. This is not
438 438 * an error --- the NULL is simply an optimization, understood by
439 439 * ld_place_path(), that allows it to skip unnecessary work.
440 440 */
441 441 Place_path_info *
442 442 ld_place_path_info_init(Ofl_desc *ofl, Ifl_desc *ifl, Place_path_info *info)
443 443 {
444 444 /*
445 445 * Return NULL if there is no input file (internally generated section)
446 446 * or if the entrance criteria list does not contain any items that will
447 447 * need to be compared to the path (all the ec_files lists are empty).
448 448 */
449 449 if ((ifl == NULL) || !(ofl->ofl_flags & FLG_OF_EC_FILES))
450 450 return (NULL);
451 451
452 452 info->ppi_path = ifl->ifl_name;
453 453 info->ppi_path_len = strlen(info->ppi_path);
454 454 info->ppi_isar = (ifl->ifl_flags & FLG_IF_EXTRACT) != 0;
455 455
456 456 /*
457 457 * The basename is the final segment of the path, equivalent to
458 458 * the path itself if there are no '/' delimiters.
459 459 */
460 460 info->ppi_bname = strrchr(info->ppi_path, '/');
461 461 if (info->ppi_bname == NULL)
462 462 info->ppi_bname = info->ppi_path;
463 463 else
464 464 info->ppi_bname++; /* Skip leading '/' */
465 465 info->ppi_bname_len =
466 466 info->ppi_path_len - (info->ppi_bname - info->ppi_path);
467 467
468 468 /*
469 469 * For an archive, the object name is the member name, which is
470 470 * enclosed in () at the end of the name string. Otherwise, it is
471 471 * the same as the basename.
472 472 */
473 473 if (info->ppi_isar) {
474 474 info->ppi_oname = strrchr(info->ppi_bname, '(');
475 475 /* There must be an archive member suffix delimited by parens */
476 476 assert((info->ppi_bname[info->ppi_bname_len - 1] == ')') &&
477 477 (info->ppi_oname != NULL));
478 478 info->ppi_oname++; /* skip leading '(' */
479 479 info->ppi_oname_len = info->ppi_bname_len -
480 480 (info->ppi_oname - info->ppi_bname + 1);
481 481 } else {
482 482 info->ppi_oname = info->ppi_bname;
483 483 info->ppi_oname_len = info->ppi_bname_len;
484 484 }
485 485
486 486 return (info);
487 487 }
488 488
489 489 /*
490 490 * Compare an input section path to the file comparison list the given
491 491 * entrance criteria.
492 492 *
493 493 * entry:
494 494 * path_info - A non-NULL Place_path_info block for the file
495 495 * containing the input section, initialized by
496 496 * ld_place_path_info_init()
497 497 * enp - Entrance criteria with a non-empty ec_files list of file
498 498 * comparisons to be carried out.
499 499 *
500 500 * exit:
501 501 * Return TRUE if a match is seen, and FALSE otherwise.
502 502 */
503 503 static Boolean
504 504 eval_ec_files(Place_path_info *path_info, Ent_desc *enp)
505 505 {
506 506 Aliste idx;
507 507 Ent_desc_file *edfp;
508 508 size_t cmp_len;
509 509 const char *cmp_str;
510 510
511 511 for (ALIST_TRAVERSE(enp->ec_files, idx, edfp)) {
512 512 Word type = edfp->edf_flags & TYP_ECF_MASK;
513 513
514 514 /*
515 515 * Determine the starting character, and # of characters,
516 516 * from the file path to compare against this entrance criteria
517 517 * file string.
518 518 */
519 519 if (type == TYP_ECF_OBJNAME) {
520 520 cmp_str = path_info->ppi_oname;
521 521 cmp_len = path_info->ppi_oname_len;
522 522 } else {
523 523 int ar_stat_diff = path_info->ppi_isar !=
524 524 ((edfp->edf_flags & FLG_ECF_ARMEMBER) != 0);
525 525
526 526 /*
527 527 * If the entrance criteria specifies an archive member
528 528 * and the file does not, then there can be no match.
529 529 */
530 530
531 531 if (ar_stat_diff && !path_info->ppi_isar)
532 532 continue;
533 533
534 534 if (type == TYP_ECF_PATH) {
535 535 cmp_str = path_info->ppi_path;
536 536 cmp_len = path_info->ppi_path_len;
537 537 } else { /* TYP_ECF_BASENAME */
538 538 cmp_str = path_info->ppi_bname;
539 539 cmp_len = path_info->ppi_bname_len;
540 540 }
541 541
542 542 /*
543 543 * If the entrance criteria does not specify an archive
544 544 * member and the file does, then a match just requires
545 545 * the paths (without the archive member) to match.
546 546 * Reduce the length to not include the ar member or
547 547 * the '(' that precedes it.
548 548 */
549 549 if (ar_stat_diff && path_info->ppi_isar)
550 550 cmp_len = path_info->ppi_oname - cmp_str - 1;
551 551 }
552 552
553 553 /*
554 554 * Compare the resulting string to the one from the
555 555 * entrance criteria.
556 556 */
557 557 if ((cmp_len == edfp->edf_name_len) &&
558 558 (strncmp(edfp->edf_name, cmp_str, cmp_len) == 0))
559 559 return (TRUE);
560 560 }
561 561
562 562 return (FALSE);
563 563 }
564 564
565 565 /*
566 566 * Replace the section header for the given input section with a new section
567 567 * header of the specified type. All values in the replacement header other
568 568 * than the type retain their previous values.
569 569 *
570 570 * entry:
571 571 * isp - Input section to replace
572 572 * sh_type - New section type to apply
573 573 *
574 574 * exit:
575 575 * Returns the pointer to the new section header on success, and
576 576 * NULL for failure.
577 577 */
578 578 static Shdr *
579 579 isp_convert_type(Is_desc *isp, Word sh_type)
580 580 {
581 581 Shdr *shdr;
582 582
583 583 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
584 584 return (NULL);
585 585 *shdr = *isp->is_shdr;
586 586 isp->is_shdr = shdr;
587 587 shdr->sh_type = sh_type;
588 588 return (shdr);
589 589 }
590 590
591 591 /*
592 592 * Issue a fatal warning for the given .eh_frame section, which
593 593 * cannot be merged with the existing .eh_frame output section.
594 594 */
595 595 static void
596 596 eh_frame_muldef(Ofl_desc *ofl, Is_desc *isp)
597 597 {
598 598 Sg_desc *sgp;
599 599 Is_desc *isp1;
600 600 Os_desc *osp;
601 601 Aliste idx1, idx2, idx3;
602 602
603 603 /*
604 604 * Locate the .eh_frame output section, and use the first section
605 605 * assigned to it in the error message. The user can then compare
606 606 * the two sections to determine what attribute prevented the merge.
607 607 */
608 608 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
609 609 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
610 610 if ((osp->os_flags & FLG_OS_EHFRAME) == 0)
611 611 continue;
612 612
613 613 for (idx3 = 0; idx3 < OS_ISD_NUM; idx3++) {
614 614 APlist *lst = osp->os_isdescs[idx3];
615 615
616 616 if (aplist_nitems(lst) == 0)
617 617 continue;
618 618
619 619 isp1 = lst->apl_data[0];
620 620 ld_eprintf(ofl, ERR_FATAL,
621 621 MSG_INTL(MSG_UPD_MULEHFRAME),
622 622 isp1->is_file->ifl_name,
623 623 EC_WORD(isp1->is_scnndx), isp1->is_name,
624 624 isp->is_file->ifl_name,
625 625 EC_WORD(isp->is_scnndx), isp->is_name);
626 626 return;
627 627 }
628 628 }
629 629 }
630 630 }
631 631
632 632 /*
633 633 * Place a section into the appropriate segment and output section.
634 634 *
635 635 * entry:
636 636 * ofl - File descriptor
637 637 * isp - Input section descriptor of section to be placed.
638 638 * path_info - NULL, or pointer to Place_path_info buffer initialized
639 639 * by ld_place_path_info_init() for the file associated to isp,
640 640 * for use in processing entrance criteria with non-empty
641 641 * file matching string list (ec_files)
642 642 * ident - Section identifier, used to order sections relative to
643 643 * others within the output segment.
644 644 * alt_os_name - If non-NULL, the name of the output section to place
645 645 * isp into. If NULL, input sections go to an output section
646 646 * with the same name as the input section.
647 647 */
648 648 Os_desc *
649 649 ld_place_section(Ofl_desc *ofl, Is_desc *isp, Place_path_info *path_info,
650 650 int ident, const char *alt_os_name)
651 651 {
652 652 Ent_desc *enp;
653 653 Sg_desc *sgp;
654 654 Os_desc *osp;
655 655 Aliste idx1, iidx;
656 656 int os_ndx;
657 657 Shdr *shdr = isp->is_shdr;
658 658 Xword shflagmask, shflags = shdr->sh_flags;
659 659 Ifl_desc *ifl = isp->is_file;
660 660 char *oname, *sname;
661 661 uint_t onamehash;
662 662 Boolean is_ehframe = (isp->is_flags & FLG_IS_EHFRAME) != 0;
663 663
664 664 /*
665 665 * Define any sections that must be thought of as referenced. These
666 666 * sections may not be referenced externally in a manner ld(1) can
667 667 * discover, but they must be retained (ie. not removed by -zignore).
668 668 */
669 669 static const Msg RefSecs[] = {
670 670 MSG_SCN_INIT, /* MSG_ORIG(MSG_SCN_INIT) */
671 671 MSG_SCN_FINI, /* MSG_ORIG(MSG_SCN_FINI) */
672 672 MSG_SCN_EX_RANGES, /* MSG_ORIG(MSG_SCN_EX_RANGES) */
673 673 MSG_SCN_EX_SHARED, /* MSG_ORIG(MSG_SCN_EX_SHARED) */
674 674 MSG_SCN_CTORS, /* MSG_ORIG(MSG_SCN_CTORS) */
675 675 MSG_SCN_DTORS, /* MSG_ORIG(MSG_SCN_DTORS) */
676 676 MSG_SCN_EHFRAME, /* MSG_ORIG(MSG_SCN_EHFRAME) */
677 677 MSG_SCN_EHFRAME_HDR, /* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */
678 678 MSG_SCN_JCR, /* MSG_ORIG(MSG_SCN_JCR) */
679 679 0
680 680 };
681 681
682 682 DBG_CALL(Dbg_sec_in(ofl->ofl_lml, isp));
683 683
684 684 /*
685 685 * If this section identifies group members, or this section indicates
686 686 * that it is a member of a group, determine whether the section is
687 687 * still required.
688 688 */
689 689 if ((shflags & SHF_GROUP) || (shdr->sh_type == SHT_GROUP)) {
690 690 Group_desc *gdesc;
691 691
692 692 if ((gdesc = ld_get_group(ofl, isp)) != NULL) {
693 693 DBG_CALL(Dbg_sec_group(ofl->ofl_lml, isp, gdesc));
694 694
695 695 /*
696 696 * If this group has been replaced by another group,
697 697 * then this section needs to be discarded.
698 698 */
699 699 if (gdesc->gd_oisc) {
700 700 isp->is_flags |= FLG_IS_DISCARD;
701 701
702 702 /*
703 703 * Since we're discarding the section, we
704 704 * can skip assigning it to an output section.
705 705 * The exception is that if the user
706 706 * specifies -z relaxreloc, then
707 707 * we need to assign the output section so
708 708 * that the sloppy relocation logic will have
709 709 * the information necessary to do its work.
710 710 */
711 711 if (!(ofl->ofl_flags1 & FLG_OF1_RLXREL))
712 712 return (NULL);
713 713 }
714 714 }
715 715
716 716 /*
717 717 * SHT_GROUP sections can only be included into relocatable
718 718 * objects.
719 719 */
720 720 if (shdr->sh_type == SHT_GROUP) {
721 721 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
722 722 isp->is_flags |= FLG_IS_DISCARD;
723 723 return (NULL);
724 724 }
725 725 }
726 726 }
727 727
728 728 /*
729 729 * Always assign SHF_TLS sections to the DATA segment (and then the
730 730 * PT_TLS embedded inside of there).
731 731 */
732 732 if (shflags & SHF_TLS)
733 733 shflags |= SHF_WRITE;
734 734
735 735 /*
736 736 * Traverse the entrance criteria list searching for a segment that
737 737 * matches the input section we have. If an entrance criterion is set
738 738 * then there must be an exact match. If we complete the loop without
739 739 * finding a segment, then sgp will be NULL.
740 740 */
741 741 sgp = NULL;
742 742 for (APLIST_TRAVERSE(ofl->ofl_ents, idx1, enp)) {
743 743
744 744 /* Disabled segments are not available for assignment */
745 745 if (enp->ec_segment->sg_flags & FLG_SG_DISABLED)
746 746 continue;
747 747
748 748 /*
749 749 * If an entrance criteria doesn't have any of its fields
750 750 * set, it will match any section it is tested against.
751 751 * We set the FLG_EC_CATCHALL flag on these, primarily because
752 752 * it helps readers of our debug output to understand what
753 753 * the criteria means --- otherwise the user would just see
754 754 * that every field is 0, but might not understand the
755 755 * significance of that.
756 756 *
757 757 * Given that we set this flag, we can use it here as an
758 758 * optimization to short circuit all of the tests in this
759 759 * loop. Note however, that if we did not do this, the end
760 760 * result would be the same --- the empty criteria will sail
761 761 * past the following tests and reach the end of the loop.
762 762 */
763 763 if (enp->ec_flags & FLG_EC_CATCHALL) {
764 764 sgp = enp->ec_segment;
765 765 break;
766 766 }
767 767
768 768 if (enp->ec_type && (enp->ec_type != shdr->sh_type))
769 769 continue;
770 770 if (enp->ec_attrmask &&
771 771 /* LINTED */
772 772 (enp->ec_attrmask & enp->ec_attrbits) !=
773 773 (enp->ec_attrmask & shflags))
774 774 continue;
775 775 if (enp->ec_is_name &&
776 776 (strcmp(enp->ec_is_name, isp->is_name) != 0))
777 777 continue;
778 778
779 779 if ((alist_nitems(enp->ec_files) > 0) &&
780 780 ((path_info == NULL) || !eval_ec_files(path_info, enp)))
781 781 continue;
782 782
783 783 /* All entrance criteria tests passed */
784 784 sgp = enp->ec_segment;
785 785 break;
786 786 }
787 787
788 788 /*
789 789 * The final entrance criteria record is a FLG_EC_CATCHALL that points
790 790 * at the final predefined segment "extra", and this final segment is
791 791 * tagged FLG_SG_NODISABLE. Therefore, the above loop must always find
792 792 * a segment.
793 793 */
794 794 assert(sgp != NULL);
795 795
796 796 /*
797 797 * Transfer the input section sorting key from the entrance criteria
798 798 * to the input section. A non-zero value means that the section
799 799 * will be sorted on this key amoung the other sections that have a
800 800 * non-zero key. These sorted sections are collectively placed at the
801 801 * head of the output section.
802 802 *
803 803 * If the sort key is 0, the section is placed after the sorted
804 804 * sections in the order they are encountered.
805 805 */
806 806 isp->is_ordndx = enp->ec_ordndx;
807 807
808 808 /* Remember that this entrance criteria has placed a section */
809 809 enp->ec_flags |= FLG_EC_USED;
810 810
811 811 /*
812 812 * If our caller has supplied an alternative name for the output
813 813 * section, then we defer to their request. Otherwise, the default
814 814 * is to use the same name as that of the input section being placed.
815 815 *
816 816 * The COMDAT, SHT_GROUP and GNU name translations that follow have
817 817 * the potential to alter this initial name.
818 818 */
819 819 oname = (char *)((alt_os_name == NULL) ? isp->is_name : alt_os_name);
820 820
821 821 /*
822 822 * Solaris section names may follow the convention:
823 823 *
824 824 * section-name%symbol-name
825 825 *
826 826 * This convention has been used to order the layout of sections within
827 827 * segments for objects built with the compilers -xF option. However,
828 828 * the final object should not contain individual section headers for
829 829 * all such input sections, instead the symbol name is stripped from the
830 830 * name to establish the final output section name.
831 831 *
832 832 * This convention has also been followed for COMDAT and sections
833 833 * identified though SHT_GROUP data.
834 834 *
835 835 * Strip out the % from the section name for:
836 836 * - Non-relocatable objects
837 837 * - Relocatable objects if input section sorting is
838 838 * in force for the segment in question.
839 839 */
840 840 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) ||
841 841 (sgp->sg_flags & FLG_SG_IS_ORDER)) {
842 842 if ((sname = strchr(isp->is_name, '%')) != NULL) {
843 843 size_t size = sname - isp->is_name;
844 844
845 845 if ((oname = libld_malloc(size + 1)) == NULL)
846 846 return ((Os_desc *)S_ERROR);
847 847 (void) strncpy(oname, isp->is_name, size);
848 848 oname[size] = '\0';
849 849 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
850 850 }
851 851 }
852 852
853 853 /*
854 854 * GNU section names may follow the convention:
855 855 *
856 856 * .gnu.linkonce.*
857 857 *
858 858 * The .gnu.linkonce is a section naming convention that indicates a
859 859 * COMDAT requirement. Determine whether this section follows the GNU
860 860 * pattern, and if so, determine whether this section should be
861 861 * discarded or retained. The comparison of is_name[1] with 'g'
862 862 * is an optimization to skip using strncmp() too much. This is safe,
863 863 * because we know the name is not NULL, and therefore must have
864 864 * at least one character plus a NULL termination.
865 865 */
866 866 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
867 867 (isp->is_name == oname) && (isp->is_name[1] == 'g') &&
868 868 (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
869 869 MSG_SCN_GNU_LINKONCE_SIZE) == 0)) {
870 870 if ((oname =
871 871 (char *)gnu_linkonce_sec(isp->is_name)) != isp->is_name) {
872 872 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
873 873 }
874 874
875 875 /*
876 876 * Explicitly identify this section type as COMDAT. Also,
877 877 * enable relaxed relocation processing, as this is typically
878 878 * a requirement with .gnu.linkonce sections.
879 879 */
880 880 isp->is_flags |= FLG_IS_COMDAT;
881 881 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
882 882 ofl->ofl_flags1 |= FLG_OF1_RLXREL;
883 883 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, TRUE,
884 884 (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0));
885 885 }
886 886
887 887 /*
888 888 * GNU section names may also follow the convention:
889 889 *
890 890 * section-name.symbol-name
891 891 *
892 892 * This convention is used when defining SHT_GROUP sections of type
893 893 * COMDAT. Thus, any group processing will have discovered any group
894 894 * sections, and this identification can be triggered by a pattern
895 895 * match section names.
896 896 */
897 897 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
898 898 (isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) &&
899 899 ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) {
900 900 size_t size = sname - isp->is_name;
901 901
902 902 if ((oname = libld_malloc(size + 1)) == NULL)
903 903 return ((Os_desc *)S_ERROR);
904 904 (void) strncpy(oname, isp->is_name, size);
905 905 oname[size] = '\0';
906 906 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
907 907
908 908 /*
909 909 * Enable relaxed relocation processing, as this is
910 910 * typically a requirement with GNU COMDAT sections.
911 911 */
912 912 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) {
913 913 ofl->ofl_flags1 |= FLG_OF1_RLXREL;
914 914 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp,
915 915 FALSE, TRUE));
916 916 }
917 917 }
918 918
919 919 /*
920 920 * Assign a hash value now that the output section name has been
921 921 * finalized.
922 922 */
923 923 onamehash = sgs_str_hash(oname);
924 924
925 925 /*
926 926 * Determine if output section ordering is turned on. If so, return
927 927 * the appropriate ordering index for the section. This information
928 928 * is derived from the Sg_desc->sg_os_order list that was built
929 929 * up from the Mapfile.
930 930 *
931 931 * A value of 0 for os_ndx means that the section is not sorted
932 932 * (i.e. is not found in the sg_os_order). The items in sg_os_order
933 933 * are in the desired sort order, so adding 1 to their alist index
934 934 * gives a suitable index for sorting.
935 935 */
936 936 os_ndx = 0;
937 937 if (alist_nitems(sgp->sg_os_order) > 0) {
938 938 Sec_order *scop;
939 939
940 940 for (ALIST_TRAVERSE(sgp->sg_os_order, idx1, scop)) {
941 941 if (strcmp(scop->sco_secname, oname) == 0) {
942 942 scop->sco_flags |= FLG_SGO_USED;
943 943 os_ndx = idx1 + 1;
944 944 break;
945 945 }
946 946 }
947 947 }
948 948
949 949 /*
950 950 * Mask of section header flags to ignore when matching sections. We
951 951 * are more strict with relocatable objects, ignoring only the order
952 952 * flags, and keeping sections apart if they differ otherwise. This
953 953 * follows the policy that sections in a relative object should only
954 954 * be merged if their flags are the same, and avoids destroying
955 955 * information prematurely. For final products however, we ignore all
956 956 * flags that do not prevent a merge.
957 957 */
958 958 shflagmask =
959 959 (ofl->ofl_flags & FLG_OF_RELOBJ) ? ALL_SHF_ORDER : ALL_SHF_IGNORE;
960 960
961 961 /*
962 962 * Traverse the input section list for the output section we have been
963 963 * assigned. If we find a matching section simply add this new section.
964 964 */
965 965 iidx = 0;
966 966 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) {
967 967 Shdr *os_shdr = osp->os_shdr;
968 968
969 969 /*
970 970 * An input section matches an output section if:
971 971 * - The ident values match
972 972 * - The names match
973 973 * - Not a GROUP section
974 974 * - Not a DTrace dof section
975 975 * - Section types match
976 976 * - Matching section flags, after screening out the
977 977 * shflagmask flags.
978 978 *
979 979 * Section types are considered to match if any one of
980 980 * the following are true:
981 981 * - The type codes are the same
982 982 * - Both are .eh_frame sections (regardless of type code)
983 983 * - The input section is COMDAT, and the output section
984 984 * is SHT_PROGBITS.
985 985 */
986 986 if ((ident == osp->os_identndx) &&
987 987 (ident != ld_targ.t_id.id_rel) &&
988 988 (onamehash == osp->os_namehash) &&
989 989 (shdr->sh_type != SHT_GROUP) &&
990 990 (shdr->sh_type != SHT_SUNW_dof) &&
991 991 ((shdr->sh_type == os_shdr->sh_type) ||
992 992 (is_ehframe && (osp->os_flags & FLG_OS_EHFRAME)) ||
993 993 ((shdr->sh_type == SHT_SUNW_COMDAT) &&
994 994 (os_shdr->sh_type == SHT_PROGBITS))) &&
995 995 ((shflags & ~shflagmask) ==
996 996 (os_shdr->sh_flags & ~shflagmask)) &&
997 997 (strcmp(oname, osp->os_name) == 0)) {
998 998 uintptr_t err;
999 999
1000 1000 /*
1001 1001 * Process any COMDAT section, keeping the first and
1002 1002 * discarding all others.
1003 1003 */
1004 1004 if ((isp->is_flags & FLG_IS_COMDAT) &&
1005 1005 ((err = add_comdat(ofl, osp, isp)) != 1))
1006 1006 return ((Os_desc *)err);
1007 1007
1008 1008 /*
1009 1009 * Set alignment
1010 1010 */
1011 1011 set_addralign(ofl, osp, isp);
1012 1012
1013 1013 /*
1014 1014 * If this section is a non-empty TLS section indicate
1015 1015 * that a PT_TLS program header is required.
1016 1016 */
1017 1017 if ((shflags & SHF_TLS) && shdr->sh_size &&
1018 1018 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
1019 1019 ofl->ofl_flags |= FLG_OF_TLSPHDR;
1020 1020
1021 1021 /*
1022 1022 * Insert the input section descriptor on the proper
1023 1023 * output section descriptor list.
1024 1024 *
1025 1025 * If this segment requires input section ordering,
1026 1026 * honor any mapfile specified ordering for otherwise
1027 1027 * unordered sections by setting the mapfile_sort
1028 1028 * argument of os_attach_isp() to True.
1029 1029 */
1030 1030
1031 1031 if (os_attach_isp(ofl, osp, isp,
1032 1032 (sgp->sg_flags & FLG_SG_IS_ORDER) != 0) == 0)
1033 1033 return ((Os_desc *)S_ERROR);
1034 1034
1035 1035 /*
1036 1036 * If this input section and file is associated to an
1037 1037 * artificially referenced output section, make sure
1038 1038 * they are marked as referenced also. This ensures
1039 1039 * that this input section and file isn't eliminated
1040 1040 * when -zignore is in effect.
1041 1041 *
1042 1042 * See -zignore comments when creating a new output
1043 1043 * section below.
1044 1044 */
1045 1045 if (((ifl &&
1046 1046 (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) &&
1047 1047 (osp->os_flags & FLG_OS_SECTREF)) {
1048 1048 isp->is_flags |= FLG_IS_SECTREF;
1049 1049 if (ifl)
1050 1050 ifl->ifl_flags |= FLG_IF_FILEREF;
1051 1051 }
1052 1052
1053 1053 DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
1054 1054 return (osp);
1055 1055 }
1056 1056
1057 1057 /*
1058 1058 * Do we need to worry about section ordering?
1059 1059 */
1060 1060 if (os_ndx) {
1061 1061 if (osp->os_ordndx) {
1062 1062 if (os_ndx < osp->os_ordndx)
1063 1063 /* insert section here. */
1064 1064 break;
1065 1065 else {
1066 1066 iidx = idx1 + 1;
1067 1067 continue;
1068 1068 }
1069 1069 } else {
1070 1070 /* insert section here. */
1071 1071 break;
1072 1072 }
1073 1073 } else if (osp->os_ordndx) {
1074 1074 iidx = idx1 + 1;
1075 1075 continue;
1076 1076 }
1077 1077
1078 1078 /*
1079 1079 * If the new sections identifier is less than that of the
1080 1080 * present input section we need to insert the new section
1081 1081 * at this point.
1082 1082 */
1083 1083 if (ident < osp->os_identndx)
1084 1084 break;
1085 1085
1086 1086 iidx = idx1 + 1;
1087 1087 }
1088 1088
1089 1089 /*
1090 1090 * We are adding a new output section. Update the section header
1091 1091 * count and associated string size.
1092 1092 *
1093 1093 * If the input section triggering this output section has been marked
1094 1094 * for discard, and if no other non-discarded input section comes along
1095 1095 * to join it, then we will over count. We cannot know if this will
1096 1096 * happen or not until all input is seen. Set FLG_OF_AJDOSCNT to
1097 1097 * trigger a final count readjustment.
1098 1098 */
1099 1099 if (isp->is_flags & FLG_IS_DISCARD)
1100 1100 ofl->ofl_flags |= FLG_OF_ADJOSCNT;
1101 1101 ofl->ofl_shdrcnt++;
1102 1102 if (st_insert(ofl->ofl_shdrsttab, oname) == -1)
1103 1103 return ((Os_desc *)S_ERROR);
1104 1104
1105 1105 /*
1106 1106 * Create a new output section descriptor.
1107 1107 */
1108 1108 if ((osp = libld_calloc(sizeof (Os_desc), 1)) == NULL)
1109 1109 return ((Os_desc *)S_ERROR);
1110 1110 if ((osp->os_shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
1111 1111 return ((Os_desc *)S_ERROR);
1112 1112
1113 1113 /*
1114 1114 * Convert COMDAT section to PROGBITS as this the first section of the
1115 1115 * output section. Save any COMDAT section for later processing, as
1116 1116 * additional COMDAT sections that match this section need discarding.
1117 1117 */
1118 1118 if ((shdr->sh_type == SHT_SUNW_COMDAT) &&
1119 1119 ((shdr = isp_convert_type(isp, SHT_PROGBITS)) == NULL))
1120 1120 return ((Os_desc *)S_ERROR);
1121 1121 if ((isp->is_flags & FLG_IS_COMDAT) &&
1122 1122 (add_comdat(ofl, osp, isp) == S_ERROR))
1123 1123 return ((Os_desc *)S_ERROR);
1124 1124
1125 1125 if (is_ehframe) {
1126 1126 /*
1127 1127 * Executable or sharable objects can have at most a single
1128 1128 * .eh_frame section. Detect attempts to create more than
1129 1129 * one. This occurs if the input sections have incompatible
1130 1130 * attributes.
1131 1131 */
1132 1132 if ((ofl->ofl_flags & FLG_OF_EHFRAME) &&
1133 1133 !(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1134 1134 eh_frame_muldef(ofl, isp);
1135 1135 return ((Os_desc *)S_ERROR);
1136 1136 }
1137 1137 ofl->ofl_flags |= FLG_OF_EHFRAME;
1138 1138
1139 1139 /*
1140 1140 * For .eh_frame sections, we always set the type to be the
1141 1141 * type specified by the ABI. This allows .eh_frame sections
1142 1142 * of type SHT_PROGBITS to be correctly merged with .eh_frame
1143 1143 * sections of the ABI-defined type (e.g. SHT_AMD64_UNWIND),
1144 1144 * with the output being of the ABI-defined type.
1145 1145 */
1146 1146 osp->os_shdr->sh_type = ld_targ.t_m.m_sht_unwind;
1147 1147 } else {
1148 1148 osp->os_shdr->sh_type = shdr->sh_type;
1149 1149 }
1150 1150
1151 1151 osp->os_shdr->sh_flags = shdr->sh_flags;
1152 1152 osp->os_shdr->sh_entsize = shdr->sh_entsize;
1153 1153 osp->os_name = oname;
1154 1154 osp->os_namehash = onamehash;
1155 1155 osp->os_ordndx = os_ndx;
1156 1156 osp->os_sgdesc = sgp;
1157 1157 if (is_ehframe)
1158 1158 osp->os_flags |= FLG_OS_EHFRAME;
1159 1159
1160 1160 if (ifl && (shdr->sh_type == SHT_PROGBITS)) {
1161 1161 /*
1162 1162 * Try to preserve the intended meaning of sh_link/sh_info.
1163 1163 * See the translate_link() in update.c.
1164 1164 */
1165 1165 osp->os_shdr->sh_link = shdr->sh_link;
1166 1166 if (shdr->sh_flags & SHF_INFO_LINK)
1167 1167 osp->os_shdr->sh_info = shdr->sh_info;
1168 1168 }
1169 1169
1170 1170 /*
1171 1171 * When -zignore is in effect, user supplied sections and files that are
1172 1172 * not referenced from other sections, are eliminated from the object
1173 1173 * being produced. Some sections, although unreferenced, are special,
1174 1174 * and must not be eliminated. Determine if this new output section is
1175 1175 * one of those special sections, and if so mark it artificially as
1176 1176 * referenced. Any input section and file associated to this output
1177 1177 * section is also be marked as referenced, and thus won't be eliminated
1178 1178 * from the final output.
1179 1179 */
1180 1180 if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) {
1181 1181 const Msg *refsec;
1182 1182
1183 1183 for (refsec = RefSecs; *refsec; refsec++) {
1184 1184 if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) {
1185 1185 osp->os_flags |= FLG_OS_SECTREF;
1186 1186
1187 1187 if ((ifl->ifl_flags & FLG_IF_IGNORE) ||
1188 1188 DBG_ENABLED) {
1189 1189 isp->is_flags |= FLG_IS_SECTREF;
1190 1190 ifl->ifl_flags |= FLG_IF_FILEREF;
1191 1191 }
1192 1192 break;
1193 1193 }
1194 1194 }
1195 1195 }
1196 1196
1197 1197 /*
1198 1198 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list,
1199 1199 * so that they can be updated as a group later.
1200 1200 */
1201 1201 if ((shdr->sh_type == SHT_GROUP) &&
1202 1202 (aplist_append(&ofl->ofl_osgroups, osp,
1203 1203 AL_CNT_OFL_OSGROUPS) == NULL))
1204 1204 return ((Os_desc *)S_ERROR);
1205 1205
1206 1206 /*
1207 1207 * If this section is a non-empty TLS section indicate that a PT_TLS
1208 1208 * program header is required.
1209 1209 */
1210 1210 if ((shflags & SHF_TLS) && shdr->sh_size &&
1211 1211 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
1212 1212 ofl->ofl_flags |= FLG_OF_TLSPHDR;
1213 1213
1214 1214 /*
1215 1215 * If a non-allocatable section is going to be put into a loadable
1216 1216 * segment then turn on the allocate bit for this section and warn the
1217 1217 * user that we have done so. This could only happen through the use
1218 1218 * of a mapfile.
1219 1219 */
1220 1220 if ((sgp->sg_phdr.p_type == PT_LOAD) &&
1221 1221 ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) {
1222 1222 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC),
1223 1223 ofl->ofl_name, osp->os_name, sgp->sg_name);
1224 1224 osp->os_shdr->sh_flags |= SHF_ALLOC;
1225 1225 }
1226 1226
1227 1227 /*
1228 1228 * Retain this sections identifier for future comparisons when placing
1229 1229 * a section (after all sections have been processed this variable will
1230 1230 * be used to hold the sections symbol index as we don't need to retain
1231 1231 * the identifier any more).
1232 1232 */
1233 1233 osp->os_identndx = ident;
1234 1234
1235 1235 /*
1236 1236 * Set alignment.
1237 1237 */
1238 1238 set_addralign(ofl, osp, isp);
1239 1239
1240 1240 if (os_attach_isp(ofl, osp, isp, 0) == 0)
1241 1241 return ((Os_desc *)S_ERROR);
1242 1242
1243 1243 DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp));
1244 1244
1245 1245 /*
1246 1246 * Insert the new section at the offset given by iidx. If no position
1247 1247 * for it was identified above, this will be index 0, causing the new
1248 1248 * section to be prepended to the beginning of the section list.
1249 1249 * Otherwise, it is the index following the section that was identified.
1250 1250 */
1251 1251 if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC,
1252 1252 iidx) == NULL)
1253 1253 return ((Os_desc *)S_ERROR);
1254 1254 return (osp);
1255 1255 }
↓ open down ↓ |
917 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX