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