Print this page
4959 completely discarded merged string sections will corrupt output objects
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/libld/common/sections.c
+++ new/usr/src/cmd/sgs/libld/common/sections.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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 27 */
28 28
29 29 /*
30 30 * Module sections. Initialize special sections
31 31 */
32 32
33 33 #define ELF_TARGET_AMD64
34 34
35 35 #include <string.h>
36 36 #include <strings.h>
37 37 #include <stdio.h>
38 38 #include <link.h>
39 39 #include <debug.h>
40 40 #include "msg.h"
41 41 #include "_libld.h"
42 42
43 43 inline static void
44 44 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
45 45 {
46 46 Sym *sym = sdp->sd_sym;
47 47 uchar_t type = ELF_ST_TYPE(sym->st_info);
48 48 /* LINTED - only used for assert() */
49 49 int err;
50 50
51 51 if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) {
52 52 ofl->ofl_locscnt--;
53 53
54 54 err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
55 55 assert(err != -1);
56 56
57 57 if (allow_ldynsym && ldynsym_symtype[type]) {
58 58 ofl->ofl_dynlocscnt--;
59 59
60 60 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
61 61 assert(err != -1);
62 62 /* Remove from sort section? */
63 63 DYNSORT_COUNT(sdp, sym, type, --);
64 64 }
65 65 }
66 66 sdp->sd_flags |= FLG_SY_ISDISC;
67 67 }
68 68
69 69 inline static void
70 70 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
71 71 {
72 72 Sym *sym = sdp->sd_sym;
73 73 uchar_t type = ELF_ST_TYPE(sym->st_info);
74 74 /* LINTED - only used for assert() */
75 75 int err;
76 76
77 77 ofl->ofl_scopecnt--;
78 78 ofl->ofl_elimcnt++;
79 79
80 80 err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
81 81 assert(err != -1);
82 82
83 83 if (allow_ldynsym && ldynsym_symtype[type]) {
84 84 ofl->ofl_dynscopecnt--;
85 85
86 86 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
87 87 assert(err != -1);
88 88 /* Remove from sort section? */
89 89 DYNSORT_COUNT(sdp, sym, type, --);
90 90 }
91 91 sdp->sd_flags |= FLG_SY_ELIM;
92 92 }
93 93
94 94 inline static void
95 95 ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym)
96 96 {
97 97 Os_desc *osp;
98 98 Is_desc *isp = sdp->sd_isc;
99 99 uchar_t bind = ELF_ST_BIND(sdp->sd_sym->st_info);
100 100
101 101 if (bind == STB_LOCAL) {
102 102 uchar_t type = ELF_ST_TYPE(sdp->sd_sym->st_info);
103 103
104 104 /*
105 105 * Skip section symbols, these were never collected in the
106 106 * first place.
107 107 */
108 108 if (type == STT_SECTION)
109 109 return;
110 110
111 111 /*
112 112 * Determine if the whole file is being removed. Remove any
113 113 * file symbol, and any symbol that is not associated with a
114 114 * section, provided the symbol has not been identified as
115 115 * (update) required.
116 116 */
117 117 if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) &&
118 118 ((type == STT_FILE) || ((isp == NULL) &&
119 119 ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) {
120 120 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
121 121 if (ifl->ifl_flags & FLG_IF_IGNORE)
122 122 remove_local(ofl, sdp, allow_ldynsym);
123 123 return;
124 124 }
125 125
126 126 } else {
127 127 /*
128 128 * Global symbols can only be eliminated when the interfaces of
129 129 * an object have been defined via versioning/scoping.
130 130 */
131 131 if (!SYM_IS_HIDDEN(sdp))
132 132 return;
133 133
134 134 /*
135 135 * Remove any unreferenced symbols that are not associated with
136 136 * a section.
137 137 */
138 138 if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) {
139 139 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
140 140 if (ifl->ifl_flags & FLG_IF_IGNORE)
141 141 remove_scoped(ofl, sdp, allow_ldynsym);
142 142 return;
143 143 }
144 144 }
145 145
146 146 /*
147 147 * Do not discard any symbols that are associated with non-allocable
148 148 * segments.
149 149 */
150 150 if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
151 151 ((osp = isp->is_osdesc) != 0) &&
152 152 (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) {
↓ open down ↓ |
152 lines elided |
↑ open up ↑ |
153 153 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
154 154 if (ifl->ifl_flags & FLG_IF_IGNORE) {
155 155 if (bind == STB_LOCAL)
156 156 remove_local(ofl, sdp, allow_ldynsym);
157 157 else
158 158 remove_scoped(ofl, sdp, allow_ldynsym);
159 159 }
160 160 }
161 161 }
162 162
163 +static Boolean
164 +isdesc_discarded(Is_desc *isp)
165 +{
166 + Ifl_desc *ifl = isp->is_file;
167 + Os_desc *osp = isp->is_osdesc;
168 + Word ptype = osp->os_sgdesc->sg_phdr.p_type;
169 +
170 + if (isp->is_flags & FLG_IS_DISCARD)
171 + return TRUE;
172 +
173 + /*
174 + * If the file is discarded, it will take
175 + * the section with it.
176 + */
177 + if (ifl &&
178 + (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
179 + ((ptype == PT_LOAD) &&
180 + ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
181 + (isp->is_shdr->sh_size > 0))) &&
182 + (ifl->ifl_flags & FLG_IF_IGNORE))
183 + return TRUE;
184 +
185 + return FALSE;
186 +}
187 +
163 188 /*
164 189 * There are situations where we may count output sections (ofl_shdrcnt)
165 190 * that are subsequently eliminated from the output object. Whether or
166 191 * not this happens cannot be known until all input has been seen and
167 192 * section elimination code has run. However, the situations where this
168 193 * outcome is possible are known, and are flagged by setting FLG_OF_ADJOSCNT.
169 194 *
170 195 * If FLG_OF_ADJOSCNT is set, this routine makes a pass over the output
171 196 * sections. If an unused output section is encountered, we decrement
172 197 * ofl->ofl_shdrcnt and remove the section name from the .shstrtab string
173 198 * table (ofl->ofl_shdrsttab).
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
174 199 *
175 200 * This code must be kept in sync with the similar code
176 201 * found in outfile.c:ld_create_outfile().
177 202 */
178 203 static void
179 204 adjust_os_count(Ofl_desc *ofl)
180 205 {
181 206 Sg_desc *sgp;
182 207 Is_desc *isp;
183 208 Os_desc *osp;
184 - Ifl_desc *ifl;
185 209 Aliste idx1;
186 210
187 211 if ((ofl->ofl_flags & FLG_OF_ADJOSCNT) == 0)
188 212 return;
189 213
190 214 /*
191 215 * For each output section, look at the input sections to find at least
192 216 * one input section that has not been eliminated. If none are found,
193 217 * the -z ignore processing above has eliminated that output section.
194 218 */
195 219 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
196 220 Aliste idx2;
197 - Word ptype = sgp->sg_phdr.p_type;
198 221
199 222 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
200 223 Aliste idx3;
201 224 int keep = 0, os_isdescs_idx;
202 225
203 226 OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) {
204 - ifl = isp->is_file;
205 -
206 - /* Input section is tagged for discard? */
207 - if (isp->is_flags & FLG_IS_DISCARD)
208 - continue;
209 -
210 - /*
211 - * If the file is discarded, it will take
212 - * the section with it.
213 - */
214 - if (ifl &&
215 - (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
216 - ((ptype == PT_LOAD) &&
217 - ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
218 - (isp->is_shdr->sh_size > 0))) &&
219 - (ifl->ifl_flags & FLG_IF_IGNORE))
220 - continue;
221 -
222 227 /*
223 228 * We have found a kept input section,
224 229 * so the output section will be created.
225 230 */
226 - keep = 1;
227 - break;
231 + if (!isdesc_discarded(isp)) {
232 + keep = 1;
233 + break;
234 + }
228 235 }
229 236 /*
230 237 * If no section of this name was kept, decrement
231 238 * the count and remove the name from .shstrtab.
232 239 */
233 240 if (keep == 0) {
234 241 /* LINTED - only used for assert() */
235 242 int err;
236 243
237 244 ofl->ofl_shdrcnt--;
238 245 err = st_delstring(ofl->ofl_shdrsttab,
239 246 osp->os_name);
240 247 assert(err != -1);
241 248 }
242 249 }
243 250 }
244 251 }
245 252
246 253 /*
247 254 * If -zignore has been in effect, scan all input files to determine if the
248 255 * file, or sections from the file, have been referenced. If not, the file or
249 256 * some of the files sections can be discarded. If sections are to be
250 257 * discarded, rescan the output relocations and the symbol table and remove
251 258 * the relocations and symbol entries that are no longer required.
252 259 *
253 260 * Note: It's possible that a section which is being discarded has contributed
254 261 * to the GOT table or the PLT table. However, we can't at this point
255 262 * eliminate the corresponding entries. This is because there could well
256 263 * be other sections referencing those same entries, but we don't have
257 264 * the infrastructure to determine this. So, keep the PLT and GOT
258 265 * entries in the table in case someone wants them.
259 266 * Note: The section to be affected needs to be allocatable.
260 267 * So even if -zignore is in effect, if the section is not allocatable,
261 268 * we do not eliminate it.
262 269 */
263 270 static uintptr_t
264 271 ignore_section_processing(Ofl_desc *ofl)
265 272 {
266 273 Sg_desc *sgp;
267 274 Is_desc *isp;
268 275 Os_desc *osp;
269 276 Ifl_desc *ifl;
270 277 Rel_cachebuf *rcbp;
271 278 Rel_desc *rsp;
272 279 int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
273 280 Aliste idx1;
274 281
275 282 for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
276 283 uint_t num, discard;
277 284
278 285 /*
279 286 * Diagnose (-D unused) a completely unreferenced file.
280 287 */
281 288 if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
282 289 DBG_CALL(Dbg_unused_file(ofl->ofl_lml,
283 290 ifl->ifl_name, 0, 0));
284 291 if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) ||
285 292 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
286 293 continue;
287 294
288 295 /*
289 296 * Before scanning the whole symbol table to determine if
290 297 * symbols should be discard - quickly (relatively) scan the
291 298 * sections to determine if any are to be discarded.
292 299 */
293 300 discard = 0;
294 301 if (ifl->ifl_flags & FLG_IF_FILEREF) {
295 302 for (num = 1; num < ifl->ifl_shnum; num++) {
296 303 if (((isp = ifl->ifl_isdesc[num]) != NULL) &&
297 304 ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
298 305 ((osp = isp->is_osdesc) != NULL) &&
299 306 ((sgp = osp->os_sgdesc) != NULL) &&
300 307 (sgp->sg_phdr.p_type == PT_LOAD)) {
301 308 discard++;
302 309 break;
303 310 }
304 311 }
305 312 }
306 313
307 314 /*
308 315 * No sections are to be 'ignored'
309 316 */
310 317 if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF))
311 318 continue;
312 319
313 320 /*
314 321 * We know that we have discarded sections. Scan the symbol
315 322 * table for this file to determine if symbols need to be
316 323 * discarded that are associated with the 'ignored' sections.
317 324 */
318 325 for (num = 1; num < ifl->ifl_symscnt; num++) {
319 326 Sym_desc *sdp;
320 327
321 328 /*
322 329 * If the symbol definition has been resolved to another
323 330 * file, or the symbol has already been discarded or
324 331 * eliminated, skip it.
325 332 */
326 333 sdp = ifl->ifl_oldndx[num];
327 334 if ((sdp->sd_file != ifl) ||
328 335 (sdp->sd_flags &
329 336 (FLG_SY_ISDISC | FLG_SY_INVALID | FLG_SY_ELIM)))
330 337 continue;
331 338
332 339 /*
333 340 * Complete the investigation of the symbol.
334 341 */
335 342 ignore_sym(ofl, ifl, sdp, allow_ldynsym);
336 343 }
337 344 }
338 345
339 346 /*
340 347 * If we were only here to solicit debugging diagnostics, we're done.
341 348 */
342 349 if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0)
343 350 return (1);
344 351
345 352 /*
346 353 * Scan all output relocations searching for those against discarded or
347 354 * ignored sections. If one is found, decrement the total outrel count.
348 355 */
349 356 REL_CACHE_TRAVERSE(&ofl->ofl_outrels, idx1, rcbp, rsp) {
350 357 Is_desc *isc = rsp->rel_isdesc;
351 358 uint_t flags, entsize;
352 359 Shdr *shdr;
353 360
354 361 if ((isc == NULL) || ((isc->is_flags & (FLG_IS_SECTREF))) ||
355 362 ((ifl = isc->is_file) == NULL) ||
356 363 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
357 364 ((shdr = isc->is_shdr) == NULL) ||
358 365 ((shdr->sh_flags & SHF_ALLOC) == 0))
359 366 continue;
360 367
361 368 flags = rsp->rel_flags;
362 369
363 370 if (flags & (FLG_REL_GOT | FLG_REL_BSS |
364 371 FLG_REL_NOINFO | FLG_REL_PLT))
365 372 continue;
366 373
367 374 osp = RELAUX_GET_OSDESC(rsp);
368 375
369 376 if (rsp->rel_flags & FLG_REL_RELA)
370 377 entsize = sizeof (Rela);
371 378 else
372 379 entsize = sizeof (Rel);
373 380
374 381 assert(osp->os_szoutrels > 0);
375 382 osp->os_szoutrels -= entsize;
376 383
377 384 if (!(flags & FLG_REL_PLT))
378 385 ofl->ofl_reloccntsub++;
379 386
380 387 if (rsp->rel_rtype == ld_targ.t_m.m_r_relative)
381 388 ofl->ofl_relocrelcnt--;
382 389 }
383 390
384 391 /*
385 392 * As a result of our work here, the number of output sections may
386 393 * have decreased. Trigger a call to adjust_os_count().
387 394 */
388 395 ofl->ofl_flags |= FLG_OF_ADJOSCNT;
389 396
390 397 return (1);
391 398 }
392 399
393 400 /*
394 401 * Allocate Elf_Data, Shdr, and Is_desc structures for a new
395 402 * section.
396 403 *
397 404 * entry:
398 405 * ofl - Output file descriptor
399 406 * shtype - SHT_ type code for section.
400 407 * shname - String giving the name for the new section.
401 408 * entcnt - # of items contained in the data part of the new section.
402 409 * This value is multiplied against the known element size
403 410 * for the section type to determine the size of the data
404 411 * area for the section. It is only meaningful in cases where
405 412 * the section type has a non-zero element size. In other cases,
406 413 * the caller must set the size fields in the *ret_data and
407 414 * *ret_shdr structs manually.
408 415 * ret_isec, ret_shdr, ret_data - Address of pointers to
409 416 * receive address of newly allocated structs.
410 417 *
411 418 * exit:
412 419 * On error, returns S_ERROR. On success, returns (1), and the
413 420 * ret_ pointers have been updated to point at the new structures,
414 421 * which have been filled in. To finish the task, the caller must
415 422 * update any fields within the supplied descriptors that differ
416 423 * from its needs, and then call ld_place_section().
417 424 */
418 425 static uintptr_t
419 426 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt,
420 427 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
421 428 {
422 429 typedef struct sec_info {
423 430 Word d_type;
424 431 Word align; /* Used in both data and section header */
425 432 Word sh_flags;
426 433 Word sh_entsize;
427 434 } SEC_INFO_T;
428 435
429 436 const SEC_INFO_T *sec_info;
430 437
431 438 Shdr *shdr;
432 439 Elf_Data *data;
433 440 Is_desc *isec;
434 441 size_t size;
435 442
436 443 /*
437 444 * For each type of section, we have a distinct set of
438 445 * SEC_INFO_T values. This macro defines a static structure
439 446 * containing those values and generates code to set the sec_info
440 447 * pointer to refer to it. The pointer in sec_info remains valid
441 448 * outside of the declaration scope because the info_s struct is static.
442 449 *
443 450 * We can't determine the value of M_WORD_ALIGN at compile time, so
444 451 * a different variant is used for those cases.
445 452 */
446 453 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
447 454 { \
448 455 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
449 456 sh_entsize}; \
450 457 sec_info = &info_s; \
451 458 }
452 459 #define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \
453 460 { \
454 461 static SEC_INFO_T info_s = { d_type, 0, sh_flags, \
455 462 sh_entsize}; \
456 463 info_s.align = ld_targ.t_m.m_word_align; \
457 464 sec_info = &info_s; \
458 465 }
459 466
460 467 switch (shtype) {
461 468 case SHT_PROGBITS:
462 469 /*
463 470 * SHT_PROGBITS sections contain are used for many
464 471 * different sections. Alignments and flags differ.
465 472 * Some have a standard entsize, and others don't.
466 473 * We set some defaults here, but there is no expectation
467 474 * that they are correct or complete for any specific
468 475 * purpose. The caller must provide the correct values.
469 476 */
470 477 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0)
471 478 break;
472 479
473 480 case SHT_SYMTAB:
474 481 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym))
475 482 break;
476 483
477 484 case SHT_DYNSYM:
478 485 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
479 486 break;
480 487
481 488 case SHT_SUNW_LDYNSYM:
482 489 ofl->ofl_flags |= FLG_OF_OSABI;
483 490 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
484 491 break;
485 492
486 493 case SHT_STRTAB:
487 494 /*
488 495 * A string table may or may not be allocable, depending
489 496 * on context, so we leave that flag unset and leave it to
490 497 * the caller to add it if necessary.
491 498 *
492 499 * String tables do not have a standard entsize, so
493 500 * we set it to 0.
494 501 */
495 502 SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0)
496 503 break;
497 504
498 505 case SHT_RELA:
499 506 /*
500 507 * Relocations with an addend (Everything except 32-bit X86).
501 508 * The caller is expected to set all section header flags.
502 509 */
503 510 SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela))
504 511 break;
505 512
506 513 case SHT_REL:
507 514 /*
508 515 * Relocations without an addend (32-bit X86 only).
509 516 * The caller is expected to set all section header flags.
510 517 */
511 518 SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel))
512 519 break;
513 520
514 521 case SHT_HASH:
515 522 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
516 523 break;
517 524
518 525 case SHT_SUNW_symsort:
519 526 case SHT_SUNW_tlssort:
520 527 ofl->ofl_flags |= FLG_OF_OSABI;
521 528 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
522 529 break;
523 530
524 531 case SHT_DYNAMIC:
525 532 /*
526 533 * A dynamic section may or may not be allocable, and may or
527 534 * may not be writable, depending on context, so we leave the
528 535 * flags unset and leave it to the caller to add them if
529 536 * necessary.
530 537 */
531 538 SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, 0, sizeof (Dyn))
532 539 break;
533 540
534 541 case SHT_NOBITS:
535 542 /*
536 543 * SHT_NOBITS is used for BSS-type sections. The size and
537 544 * alignment depend on the specific use and must be adjusted
538 545 * by the caller.
539 546 */
540 547 SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0)
541 548 break;
542 549
543 550 case SHT_INIT_ARRAY:
544 551 case SHT_FINI_ARRAY:
545 552 case SHT_PREINIT_ARRAY:
546 553 SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE,
547 554 sizeof (Addr))
548 555 break;
549 556
550 557 case SHT_SYMTAB_SHNDX:
551 558 /*
552 559 * Note that these sections are created to be associated
553 560 * with both symtab and dynsym symbol tables. However, they
554 561 * are non-allocable in all cases, because the runtime
555 562 * linker has no need for this information. It is purely
556 563 * informational, used by elfdump(1), debuggers, etc.
557 564 */
558 565 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word));
559 566 break;
560 567
561 568 case SHT_SUNW_cap:
562 569 ofl->ofl_flags |= FLG_OF_OSABI;
563 570 SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap));
564 571 break;
565 572
566 573 case SHT_SUNW_capchain:
567 574 ofl->ofl_flags |= FLG_OF_OSABI;
568 575 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC,
569 576 sizeof (Capchain));
570 577 break;
571 578
572 579 case SHT_SUNW_capinfo:
573 580 ofl->ofl_flags |= FLG_OF_OSABI;
574 581 #if _ELF64
575 582 SET_SEC_INFO(ELF_T_XWORD, sizeof (Xword), SHF_ALLOC,
576 583 sizeof (Capinfo));
577 584 #else
578 585 SET_SEC_INFO(ELF_T_WORD, sizeof (Word), SHF_ALLOC,
579 586 sizeof (Capinfo));
580 587 #endif
581 588 break;
582 589
583 590 case SHT_SUNW_move:
584 591 ofl->ofl_flags |= FLG_OF_OSABI;
585 592 SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword),
586 593 SHF_ALLOC | SHF_WRITE, sizeof (Move));
587 594 break;
588 595
589 596 case SHT_SUNW_syminfo:
590 597 ofl->ofl_flags |= FLG_OF_OSABI;
591 598 /*
592 599 * The sh_info field of the SHT_*_syminfo section points
593 600 * to the header index of the associated .dynamic section,
594 601 * so we also set SHF_INFO_LINK.
595 602 */
596 603 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE,
597 604 SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo));
598 605 break;
599 606
600 607 case SHT_SUNW_verneed:
601 608 case SHT_SUNW_verdef:
602 609 ofl->ofl_flags |= FLG_OF_OSABI;
603 610 /*
604 611 * The info for verneed and versym happen to be the same.
605 612 * The entries in these sections are not of uniform size,
606 613 * so we set the entsize to 0.
607 614 */
608 615 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0);
609 616 break;
610 617
611 618 case SHT_SUNW_versym:
612 619 ofl->ofl_flags |= FLG_OF_OSABI;
613 620 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC,
614 621 sizeof (Versym));
615 622 break;
616 623
617 624 default:
618 625 /* Should not happen: fcn called with unknown section type */
619 626 assert(0);
620 627 return (S_ERROR);
621 628 }
622 629 #undef SET_SEC_INFO
623 630 #undef SET_SEC_INFO_WORD_ALIGN
624 631
625 632 size = entcnt * sec_info->sh_entsize;
626 633
627 634 /*
628 635 * Allocate and initialize the Elf_Data structure.
629 636 */
630 637 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
631 638 return (S_ERROR);
632 639 data->d_type = sec_info->d_type;
633 640 data->d_size = size;
634 641 data->d_align = sec_info->align;
635 642 data->d_version = ofl->ofl_dehdr->e_version;
636 643
637 644 /*
638 645 * Allocate and initialize the Shdr structure.
639 646 */
640 647 if ((shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
641 648 return (S_ERROR);
642 649 shdr->sh_type = shtype;
643 650 shdr->sh_size = size;
644 651 shdr->sh_flags = sec_info->sh_flags;
645 652 shdr->sh_addralign = sec_info->align;
646 653 shdr->sh_entsize = sec_info->sh_entsize;
647 654
648 655 /*
649 656 * Allocate and initialize the Is_desc structure.
650 657 */
651 658 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
652 659 return (S_ERROR);
653 660 isec->is_name = shname;
654 661 isec->is_shdr = shdr;
655 662 isec->is_indata = data;
656 663
657 664
658 665 *ret_isec = isec;
659 666 *ret_shdr = shdr;
660 667 *ret_data = data;
661 668 return (1);
662 669 }
663 670
664 671 /*
665 672 * Use an existing input section as a template to create a new
666 673 * input section with the same values as the original, other than
667 674 * the size of the data area which is supplied by the caller.
668 675 *
669 676 * entry:
670 677 * ofl - Output file descriptor
671 678 * ifl - Input file section to use as a template
672 679 * size - Size of data area for new section
673 680 * ret_isec, ret_shdr, ret_data - Address of pointers to
674 681 * receive address of newly allocated structs.
675 682 *
676 683 * exit:
677 684 * On error, returns S_ERROR. On success, returns (1), and the
678 685 * ret_ pointers have been updated to point at the new structures,
679 686 * which have been filled in. To finish the task, the caller must
680 687 * update any fields within the supplied descriptors that differ
681 688 * from its needs, and then call ld_place_section().
682 689 */
683 690 static uintptr_t
684 691 new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size,
685 692 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
686 693 {
687 694 Shdr *shdr;
688 695 Elf_Data *data;
689 696 Is_desc *isec;
690 697
691 698 /*
692 699 * Allocate and initialize the Elf_Data structure.
693 700 */
694 701 if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
695 702 return (S_ERROR);
696 703 data->d_type = tmpl_isp->is_indata->d_type;
697 704 data->d_size = size;
698 705 data->d_align = tmpl_isp->is_shdr->sh_addralign;
699 706 data->d_version = ofl->ofl_dehdr->e_version;
700 707
701 708 /*
702 709 * Allocate and initialize the Shdr structure.
703 710 */
704 711 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
705 712 return (S_ERROR);
706 713 *shdr = *tmpl_isp->is_shdr;
707 714 shdr->sh_addr = 0;
708 715 shdr->sh_offset = 0;
709 716 shdr->sh_size = size;
710 717
711 718 /*
712 719 * Allocate and initialize the Is_desc structure.
713 720 */
714 721 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
715 722 return (S_ERROR);
716 723 isec->is_name = tmpl_isp->is_name;
717 724 isec->is_shdr = shdr;
718 725 isec->is_indata = data;
719 726
720 727
721 728 *ret_isec = isec;
722 729 *ret_shdr = shdr;
723 730 *ret_data = data;
724 731 return (1);
725 732 }
726 733
727 734 /*
728 735 * Build a .bss section for allocation of tentative definitions. Any `static'
729 736 * .bss definitions would have been associated to their own .bss sections and
730 737 * thus collected from the input files. `global' .bss definitions are tagged
731 738 * as COMMON and do not cause any associated .bss section elements to be
732 739 * generated. Here we add up all these COMMON symbols and generate the .bss
733 740 * section required to represent them.
734 741 */
735 742 uintptr_t
736 743 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, uint_t ident)
737 744 {
738 745 Shdr *shdr;
739 746 Elf_Data *data;
740 747 Is_desc *isec;
741 748 Os_desc *osp;
742 749 Xword rsize = (Xword)ofl->ofl_relocbsssz;
743 750
744 751 /*
745 752 * Allocate header structs. We will set the name ourselves below,
746 753 * and there is no entcnt for a BSS. So, the shname and entcnt
747 754 * arguments are 0.
748 755 */
749 756 if (new_section(ofl, SHT_NOBITS, NULL, 0,
750 757 &isec, &shdr, &data) == S_ERROR)
751 758 return (S_ERROR);
752 759
753 760 data->d_size = (size_t)size;
754 761 data->d_align = (size_t)align;
755 762
756 763 shdr->sh_size = size;
757 764 shdr->sh_addralign = align;
758 765
759 766 if (ident == ld_targ.t_id.id_tlsbss) {
760 767 isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
761 768 ofl->ofl_istlsbss = isec;
762 769 shdr->sh_flags |= SHF_TLS;
763 770
764 771 } else if (ident == ld_targ.t_id.id_bss) {
765 772 isec->is_name = MSG_ORIG(MSG_SCN_BSS);
766 773 ofl->ofl_isbss = isec;
767 774
768 775 #if defined(_ELF64)
769 776 } else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
770 777 (ident == ld_targ.t_id.id_lbss)) {
771 778 isec->is_name = MSG_ORIG(MSG_SCN_LBSS);
772 779 ofl->ofl_islbss = isec;
773 780 shdr->sh_flags |= SHF_AMD64_LARGE;
774 781 #endif
775 782 }
776 783
777 784 /*
778 785 * Retain this .*bss input section as this will be where global symbol
779 786 * references are added.
780 787 */
781 788 if ((osp = ld_place_section(ofl, isec, NULL, ident, NULL)) ==
782 789 (Os_desc *)S_ERROR)
783 790 return (S_ERROR);
784 791
785 792 /*
786 793 * If relocations exist against a .*bss section, a section symbol must
787 794 * be created for the section in the .dynsym symbol table.
788 795 */
789 796 if (!(osp->os_flags & FLG_OS_OUTREL)) {
790 797 ofl_flag_t flagtotest;
791 798
792 799 if (ident == ld_targ.t_id.id_tlsbss)
793 800 flagtotest = FLG_OF1_TLSOREL;
794 801 else
795 802 flagtotest = FLG_OF1_BSSOREL;
796 803
797 804 if (ofl->ofl_flags1 & flagtotest) {
798 805 ofl->ofl_dynshdrcnt++;
799 806 osp->os_flags |= FLG_OS_OUTREL;
800 807 }
801 808 }
802 809
803 810 osp->os_szoutrels = rsize;
804 811 return (1);
805 812 }
806 813
807 814 /*
808 815 * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
809 816 * ld -z *array=name).
810 817 */
811 818 static uintptr_t
812 819 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, APlist *alp)
813 820 {
814 821 uint_t entcount;
815 822 Aliste idx;
816 823 Elf_Data *data;
817 824 Is_desc *isec;
818 825 Shdr *shdr;
819 826 Sym_desc *sdp;
820 827 Rel_desc reld;
821 828 Rela reloc;
822 829 Os_desc *osp;
823 830 uintptr_t ret = 1;
824 831
825 832 if (alp == NULL)
826 833 return (1);
827 834
828 835 entcount = 0;
829 836 for (APLIST_TRAVERSE(alp, idx, sdp))
830 837 entcount++;
831 838
832 839 if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
833 840 S_ERROR)
834 841 return (S_ERROR);
835 842
836 843 if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == NULL)
837 844 return (S_ERROR);
838 845
839 846 if (ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_array, NULL) ==
840 847 (Os_desc *)S_ERROR)
841 848 return (S_ERROR);
842 849
843 850 osp = isec->is_osdesc;
844 851
845 852 if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY))
846 853 ofl->ofl_osinitarray = osp;
847 854 if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY))
848 855 ofl->ofl_ospreinitarray = osp;
849 856 else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY))
850 857 ofl->ofl_osfiniarray = osp;
851 858
852 859 /*
853 860 * Create relocations against this section to initialize it to the
854 861 * function addresses.
855 862 */
856 863 reld.rel_isdesc = isec;
857 864 reld.rel_aux = NULL;
858 865 reld.rel_flags = FLG_REL_LOAD;
859 866
860 867 /*
861 868 * Fabricate the relocation information (as if a relocation record had
862 869 * been input - see init_rel()).
863 870 */
864 871 reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr;
865 872 reld.rel_roffset = 0;
866 873 reld.rel_raddend = 0;
867 874
868 875 /*
869 876 * Create a minimal relocation record to satisfy process_sym_reloc()
870 877 * debugging requirements.
871 878 */
872 879 reloc.r_offset = 0;
873 880 reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr);
874 881 reloc.r_addend = 0;
875 882
876 883 DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp,
877 884 ld_targ.t_m.m_rel_sht_type));
878 885 for (APLIST_TRAVERSE(alp, idx, sdp)) {
879 886 reld.rel_sym = sdp;
880 887
881 888 if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
882 889 MSG_INTL(MSG_STR_COMMAND), 0) == S_ERROR) {
883 890 ret = S_ERROR;
884 891 continue;
885 892 }
886 893
887 894 reld.rel_roffset += (Xword)sizeof (Addr);
888 895 reloc.r_offset = reld.rel_roffset;
889 896 }
890 897
891 898 return (ret);
892 899 }
893 900
894 901 /*
895 902 * Build a comment section (-Qy option).
896 903 */
897 904 static uintptr_t
898 905 make_comment(Ofl_desc *ofl)
899 906 {
900 907 Shdr *shdr;
901 908 Elf_Data *data;
902 909 Is_desc *isec;
903 910
904 911 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
905 912 &isec, &shdr, &data) == S_ERROR)
906 913 return (S_ERROR);
907 914
908 915 data->d_buf = (void *)ofl->ofl_sgsid;
909 916 data->d_size = strlen(ofl->ofl_sgsid) + 1;
910 917 data->d_align = 1;
911 918
912 919 shdr->sh_size = (Xword)data->d_size;
913 920 shdr->sh_flags = 0;
914 921 shdr->sh_addralign = 1;
915 922
916 923 return ((uintptr_t)ld_place_section(ofl, isec, NULL,
917 924 ld_targ.t_id.id_note, NULL));
918 925 }
919 926
920 927 /*
921 928 * Make the dynamic section. Calculate the size of any strings referenced
922 929 * within this structure, they will be added to the global string table
923 930 * (.dynstr). This routine should be called before make_dynstr().
924 931 *
925 932 * This routine must be maintained in parallel with update_odynamic()
926 933 * in update.c
927 934 */
928 935 static uintptr_t
929 936 make_dynamic(Ofl_desc *ofl)
930 937 {
931 938 Shdr *shdr;
932 939 Os_desc *osp;
933 940 Elf_Data *data;
934 941 Is_desc *isec;
935 942 size_t cnt = 0;
936 943 Aliste idx;
937 944 Ifl_desc *ifl;
938 945 Sym_desc *sdp;
939 946 size_t size;
940 947 Str_tbl *strtbl;
941 948 ofl_flag_t flags = ofl->ofl_flags;
942 949 int not_relobj = !(flags & FLG_OF_RELOBJ);
943 950 int unused = 0;
944 951
945 952 /*
946 953 * Select the required string table.
947 954 */
948 955 if (OFL_IS_STATIC_OBJ(ofl))
949 956 strtbl = ofl->ofl_strtab;
950 957 else
951 958 strtbl = ofl->ofl_dynstrtab;
952 959
953 960 /*
954 961 * Only a limited subset of DT_ entries apply to relocatable
955 962 * objects. See the comment at the head of update_odynamic() in
956 963 * update.c for details.
957 964 */
958 965 if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
959 966 &isec, &shdr, &data) == S_ERROR)
960 967 return (S_ERROR);
961 968
962 969 /*
963 970 * new_section() does not set SHF_ALLOC. If we're building anything
964 971 * besides a relocatable object, then the .dynamic section should
965 972 * reside in allocatable memory.
966 973 */
967 974 if (not_relobj)
968 975 shdr->sh_flags |= SHF_ALLOC;
969 976
970 977 /*
971 978 * new_section() does not set SHF_WRITE. If we're building an object
972 979 * that specifies an interpretor, then a DT_DEBUG entry is created,
973 980 * which is initialized to the applications link-map list at runtime.
974 981 */
975 982 if (ofl->ofl_osinterp)
976 983 shdr->sh_flags |= SHF_WRITE;
977 984
978 985 osp = ofl->ofl_osdynamic =
979 986 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynamic, NULL);
980 987
981 988 /*
982 989 * Reserve entries for any needed dependencies.
983 990 */
984 991 for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
985 992 if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
986 993 continue;
987 994
988 995 /*
989 996 * If this dependency didn't satisfy any symbol references,
990 997 * generate a debugging diagnostic (ld(1) -Dunused can be used
991 998 * to display these). If this is a standard needed dependency,
992 999 * and -z ignore is in effect, drop the dependency. Explicitly
993 1000 * defined dependencies (i.e., -N dep) don't get dropped, and
994 1001 * are flagged as being required to simplify update_odynamic()
995 1002 * processing.
996 1003 */
997 1004 if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
998 1005 ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
999 1006 if (unused++ == 0)
1000 1007 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1001 1008 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
1002 1009 (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
1003 1010
1004 1011 /*
1005 1012 * Guidance: Remove unused dependency.
1006 1013 *
1007 1014 * If -z ignore is in effect, this warning is not
1008 1015 * needed because we will quietly remove the unused
1009 1016 * dependency.
1010 1017 */
1011 1018 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_UNUSED) &&
1012 1019 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
1013 1020 ld_eprintf(ofl, ERR_GUIDANCE,
1014 1021 MSG_INTL(MSG_GUIDE_UNUSED),
1015 1022 ifl->ifl_soname);
1016 1023
1017 1024 if (ifl->ifl_flags & FLG_IF_NEEDSTR)
1018 1025 ifl->ifl_flags |= FLG_IF_DEPREQD;
1019 1026 else if (ifl->ifl_flags & FLG_IF_IGNORE)
1020 1027 continue;
1021 1028 }
1022 1029
1023 1030 /*
1024 1031 * If this object requires a DT_POSFLAG_1 entry, reserve it.
1025 1032 */
1026 1033 if ((ifl->ifl_flags & MSK_IF_POSFLAG1) && not_relobj)
1027 1034 cnt++;
1028 1035
1029 1036 if (st_insert(strtbl, ifl->ifl_soname) == -1)
1030 1037 return (S_ERROR);
1031 1038 cnt++;
1032 1039
1033 1040 /*
1034 1041 * If the needed entry contains the $ORIGIN token make sure
1035 1042 * the associated DT_1_FLAGS entry is created.
1036 1043 */
1037 1044 if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
1038 1045 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1039 1046 ofl->ofl_dtflags |= DF_ORIGIN;
1040 1047 }
1041 1048 }
1042 1049
1043 1050 if (unused)
1044 1051 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1045 1052
1046 1053 if (not_relobj) {
1047 1054 /*
1048 1055 * Reserve entries for any per-symbol auxiliary/filter strings.
1049 1056 */
1050 1057 cnt += alist_nitems(ofl->ofl_dtsfltrs);
1051 1058
1052 1059 /*
1053 1060 * Reserve entries for _init() and _fini() section addresses.
1054 1061 */
1055 1062 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1056 1063 SYM_NOHASH, NULL, ofl)) != NULL) &&
1057 1064 (sdp->sd_ref == REF_REL_NEED) &&
1058 1065 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1059 1066 sdp->sd_flags |= FLG_SY_UPREQD;
1060 1067 cnt++;
1061 1068 }
1062 1069 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1063 1070 SYM_NOHASH, NULL, ofl)) != NULL) &&
1064 1071 (sdp->sd_ref == REF_REL_NEED) &&
1065 1072 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1066 1073 sdp->sd_flags |= FLG_SY_UPREQD;
1067 1074 cnt++;
1068 1075 }
1069 1076
1070 1077 /*
1071 1078 * Reserve entries for any soname, filter name (shared libs
1072 1079 * only), run-path pointers, cache names and audit requirements.
1073 1080 */
1074 1081 if (ofl->ofl_soname) {
1075 1082 cnt++;
1076 1083 if (st_insert(strtbl, ofl->ofl_soname) == -1)
1077 1084 return (S_ERROR);
1078 1085 }
1079 1086 if (ofl->ofl_filtees) {
1080 1087 cnt++;
1081 1088 if (st_insert(strtbl, ofl->ofl_filtees) == -1)
1082 1089 return (S_ERROR);
1083 1090
1084 1091 /*
1085 1092 * If the filtees entry contains the $ORIGIN token
1086 1093 * make sure the associated DT_1_FLAGS entry is created.
1087 1094 */
1088 1095 if (strstr(ofl->ofl_filtees,
1089 1096 MSG_ORIG(MSG_STR_ORIGIN))) {
1090 1097 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1091 1098 ofl->ofl_dtflags |= DF_ORIGIN;
1092 1099 }
1093 1100 }
1094 1101 }
1095 1102
1096 1103 if (ofl->ofl_rpath) {
1097 1104 cnt += 2; /* DT_RPATH & DT_RUNPATH */
1098 1105 if (st_insert(strtbl, ofl->ofl_rpath) == -1)
1099 1106 return (S_ERROR);
1100 1107
1101 1108 /*
1102 1109 * If the rpath entry contains the $ORIGIN token make sure
1103 1110 * the associated DT_1_FLAGS entry is created.
1104 1111 */
1105 1112 if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
1106 1113 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1107 1114 ofl->ofl_dtflags |= DF_ORIGIN;
1108 1115 }
1109 1116 }
1110 1117
1111 1118 if (not_relobj) {
1112 1119 Aliste idx;
1113 1120 Sg_desc *sgp;
1114 1121
1115 1122 if (ofl->ofl_config) {
1116 1123 cnt++;
1117 1124 if (st_insert(strtbl, ofl->ofl_config) == -1)
1118 1125 return (S_ERROR);
1119 1126
1120 1127 /*
1121 1128 * If the config entry contains the $ORIGIN token
1122 1129 * make sure the associated DT_1_FLAGS entry is created.
1123 1130 */
1124 1131 if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
1125 1132 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1126 1133 ofl->ofl_dtflags |= DF_ORIGIN;
1127 1134 }
1128 1135 }
1129 1136 if (ofl->ofl_depaudit) {
1130 1137 cnt++;
1131 1138 if (st_insert(strtbl, ofl->ofl_depaudit) == -1)
1132 1139 return (S_ERROR);
1133 1140 }
1134 1141 if (ofl->ofl_audit) {
1135 1142 cnt++;
1136 1143 if (st_insert(strtbl, ofl->ofl_audit) == -1)
1137 1144 return (S_ERROR);
1138 1145 }
1139 1146
1140 1147 /*
1141 1148 * Reserve entries for the DT_HASH, DT_STRTAB, DT_STRSZ,
1142 1149 * DT_SYMTAB, DT_SYMENT, and DT_CHECKSUM.
1143 1150 */
1144 1151 cnt += 6;
1145 1152
1146 1153 /*
1147 1154 * If we are including local functions at the head of
1148 1155 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1149 1156 * and DT_SUNW_SYMSZ.
1150 1157 */
1151 1158 if (OFL_ALLOW_LDYNSYM(ofl))
1152 1159 cnt += 2;
1153 1160
1154 1161 if ((ofl->ofl_dynsymsortcnt > 0) ||
1155 1162 (ofl->ofl_dyntlssortcnt > 0))
1156 1163 cnt++; /* DT_SUNW_SORTENT */
1157 1164
1158 1165 if (ofl->ofl_dynsymsortcnt > 0)
1159 1166 cnt += 2; /* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1160 1167
1161 1168 if (ofl->ofl_dyntlssortcnt > 0)
1162 1169 cnt += 2; /* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1163 1170
1164 1171 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1165 1172 FLG_OF_VERDEF)
1166 1173 cnt += 2; /* DT_VERDEF & DT_VERDEFNUM */
1167 1174
1168 1175 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1169 1176 FLG_OF_VERNEED)
1170 1177 cnt += 2; /* DT_VERNEED & DT_VERNEEDNUM */
1171 1178
1172 1179 if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
1173 1180 cnt++; /* DT_RELACOUNT */
1174 1181
1175 1182 if (flags & FLG_OF_TEXTREL) /* DT_TEXTREL */
1176 1183 cnt++;
1177 1184
1178 1185 if (ofl->ofl_osfiniarray) /* DT_FINI_ARRAY */
1179 1186 cnt += 2; /* DT_FINI_ARRAYSZ */
1180 1187
1181 1188 if (ofl->ofl_osinitarray) /* DT_INIT_ARRAY */
1182 1189 cnt += 2; /* DT_INIT_ARRAYSZ */
1183 1190
1184 1191 if (ofl->ofl_ospreinitarray) /* DT_PREINIT_ARRAY & */
1185 1192 cnt += 2; /* DT_PREINIT_ARRAYSZ */
1186 1193
1187 1194 /*
1188 1195 * If we have plt's reserve a DT_PLTRELSZ, DT_PLTREL and
1189 1196 * DT_JMPREL.
1190 1197 */
1191 1198 if (ofl->ofl_pltcnt)
1192 1199 cnt += 3;
1193 1200
1194 1201 /*
1195 1202 * If plt padding is needed (Sparcv9).
1196 1203 */
1197 1204 if (ofl->ofl_pltpad)
1198 1205 cnt += 2; /* DT_PLTPAD & DT_PLTPADSZ */
1199 1206
1200 1207 /*
1201 1208 * If we have any relocations reserve a DT_REL, DT_RELSZ and
1202 1209 * DT_RELENT entry.
1203 1210 */
1204 1211 if (ofl->ofl_relocsz)
1205 1212 cnt += 3;
1206 1213
1207 1214 /*
1208 1215 * If a syminfo section is required create DT_SYMINFO,
1209 1216 * DT_SYMINSZ, and DT_SYMINENT entries.
1210 1217 */
1211 1218 if (flags & FLG_OF_SYMINFO)
1212 1219 cnt += 3;
1213 1220
1214 1221 /*
1215 1222 * If there are any partially initialized sections allocate
1216 1223 * DT_MOVETAB, DT_MOVESZ and DT_MOVEENT.
1217 1224 */
1218 1225 if (ofl->ofl_osmove)
1219 1226 cnt += 3;
1220 1227
1221 1228 /*
1222 1229 * Allocate one DT_REGISTER entry for every register symbol.
1223 1230 */
1224 1231 cnt += ofl->ofl_regsymcnt;
1225 1232
1226 1233 /*
1227 1234 * Reserve a entry for each '-zrtldinfo=...' specified
1228 1235 * on the command line.
1229 1236 */
1230 1237 for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp))
1231 1238 cnt++;
1232 1239
1233 1240 /*
1234 1241 * The following entry should only be placed in a segment that
1235 1242 * is writable.
1236 1243 */
1237 1244 if (((sgp = osp->os_sgdesc) != NULL) &&
1238 1245 (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp)
1239 1246 cnt++; /* DT_DEBUG */
1240 1247
1241 1248 /*
1242 1249 * Capabilities require a .dynamic entry for the .SUNW_cap
1243 1250 * section.
1244 1251 */
1245 1252 if (ofl->ofl_oscap)
1246 1253 cnt++; /* DT_SUNW_CAP */
1247 1254
1248 1255 /*
1249 1256 * Symbol capabilities require a .dynamic entry for the
1250 1257 * .SUNW_capinfo section.
1251 1258 */
1252 1259 if (ofl->ofl_oscapinfo)
1253 1260 cnt++; /* DT_SUNW_CAPINFO */
1254 1261
1255 1262 /*
1256 1263 * Capabilities chain information requires a .SUNW_capchain
1257 1264 * entry (DT_SUNW_CAPCHAIN), entry size (DT_SUNW_CAPCHAINENT),
1258 1265 * and total size (DT_SUNW_CAPCHAINSZ).
1259 1266 */
1260 1267 if (ofl->ofl_oscapchain)
1261 1268 cnt += 3;
1262 1269
1263 1270 if (flags & FLG_OF_SYMBOLIC)
1264 1271 cnt++; /* DT_SYMBOLIC */
1265 1272 }
1266 1273
1267 1274 /*
1268 1275 * Account for Architecture dependent .dynamic entries, and defaults.
1269 1276 */
1270 1277 (*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt);
1271 1278
1272 1279 /*
1273 1280 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1274 1281 * allow room for the unused extra DT_NULLs. These are included
1275 1282 * to allow an ELF editor room to add items later.
1276 1283 */
1277 1284 cnt += 4 + DYNAMIC_EXTRA_ELTS;
1278 1285
1279 1286 /*
1280 1287 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1281 1288 * linker that produced the output object. This information
1282 1289 * allows us to determine whether a given object was linked
1283 1290 * natively, or by a linker running on a different type of
1284 1291 * system. This information can be valuable if one suspects
1285 1292 * that a problem might be due to alignment or byte order issues.
1286 1293 */
1287 1294 cnt++;
1288 1295
1289 1296 /*
1290 1297 * Determine the size of the section from the number of entries.
1291 1298 */
1292 1299 size = cnt * (size_t)shdr->sh_entsize;
1293 1300
1294 1301 shdr->sh_size = (Xword)size;
1295 1302 data->d_size = size;
1296 1303
1297 1304 /*
1298 1305 * There are several tags that are specific to the Solaris osabi
1299 1306 * range which we unconditionally put into any dynamic section
1300 1307 * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such,
1301 1308 * any Solaris object with a dynamic section should be tagged as
1302 1309 * ELFOSABI_SOLARIS.
1303 1310 */
1304 1311 ofl->ofl_flags |= FLG_OF_OSABI;
1305 1312
1306 1313 return ((uintptr_t)ofl->ofl_osdynamic);
1307 1314 }
1308 1315
1309 1316 /*
1310 1317 * Build the GOT section and its associated relocation entries.
1311 1318 */
1312 1319 uintptr_t
1313 1320 ld_make_got(Ofl_desc *ofl)
1314 1321 {
1315 1322 Elf_Data *data;
1316 1323 Shdr *shdr;
1317 1324 Is_desc *isec;
1318 1325 size_t size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize;
1319 1326 size_t rsize = (size_t)ofl->ofl_relocgotsz;
1320 1327
1321 1328 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1322 1329 &isec, &shdr, &data) == S_ERROR)
1323 1330 return (S_ERROR);
1324 1331
1325 1332 data->d_size = size;
1326 1333
1327 1334 shdr->sh_flags |= SHF_WRITE;
1328 1335 shdr->sh_size = (Xword)size;
1329 1336 shdr->sh_entsize = ld_targ.t_m.m_got_entsize;
1330 1337
1331 1338 ofl->ofl_osgot = ld_place_section(ofl, isec, NULL,
1332 1339 ld_targ.t_id.id_got, NULL);
1333 1340 if (ofl->ofl_osgot == (Os_desc *)S_ERROR)
1334 1341 return (S_ERROR);
1335 1342
1336 1343 ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1337 1344
1338 1345 return (1);
1339 1346 }
1340 1347
1341 1348 /*
1342 1349 * Build an interpreter section.
1343 1350 */
1344 1351 static uintptr_t
1345 1352 make_interp(Ofl_desc *ofl)
1346 1353 {
1347 1354 Shdr *shdr;
1348 1355 Elf_Data *data;
1349 1356 Is_desc *isec;
1350 1357 const char *iname = ofl->ofl_interp;
1351 1358 size_t size;
1352 1359
1353 1360 /*
1354 1361 * If -z nointerp is in effect, don't create an interpreter section.
1355 1362 */
1356 1363 if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1357 1364 return (1);
1358 1365
1359 1366 /*
1360 1367 * An .interp section is always created for a dynamic executable.
1361 1368 * A user can define the interpreter to use. This definition overrides
1362 1369 * the default that would be recorded in an executable, and triggers
1363 1370 * the creation of an .interp section in any other object. Presumably
1364 1371 * the user knows what they are doing. Refer to the generic ELF ABI
1365 1372 * section 5-4, and the ld(1) -I option.
1366 1373 */
1367 1374 if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1368 1375 FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1369 1376 return (1);
1370 1377
1371 1378 /*
1372 1379 * In the case of a dynamic executable, supply a default interpreter
1373 1380 * if the user has not specified their own.
1374 1381 */
1375 1382 if (iname == NULL)
1376 1383 iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp;
1377 1384
1378 1385 size = strlen(iname) + 1;
1379 1386
1380 1387 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1381 1388 &isec, &shdr, &data) == S_ERROR)
1382 1389 return (S_ERROR);
1383 1390
1384 1391 data->d_size = size;
1385 1392 shdr->sh_size = (Xword)size;
1386 1393 data->d_align = shdr->sh_addralign = 1;
1387 1394
1388 1395 ofl->ofl_osinterp =
1389 1396 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_interp, NULL);
1390 1397 return ((uintptr_t)ofl->ofl_osinterp);
1391 1398 }
1392 1399
1393 1400 /*
1394 1401 * Common function used to build the SHT_SUNW_versym section, SHT_SUNW_syminfo
1395 1402 * section, and SHT_SUNW_capinfo section. Each of these sections provide
1396 1403 * additional symbol information, and their size parallels the associated
1397 1404 * symbol table.
1398 1405 */
1399 1406 static Os_desc *
1400 1407 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
1401 1408 {
1402 1409 Shdr *shdr;
1403 1410 Elf_Data *data;
1404 1411 Is_desc *isec;
1405 1412
1406 1413 /*
1407 1414 * We don't know the size of this section yet, so set it to 0. The
1408 1415 * size gets filled in after the associated symbol table is sized.
1409 1416 */
1410 1417 if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
1411 1418 S_ERROR)
1412 1419 return ((Os_desc *)S_ERROR);
1413 1420
1414 1421 return (ld_place_section(ofl, isec, NULL, ident, NULL));
1415 1422 }
1416 1423
1417 1424 /*
1418 1425 * Determine whether a symbol capability is redundant because the object
1419 1426 * capabilities are more restrictive.
1420 1427 */
1421 1428 inline static int
1422 1429 is_cap_redundant(Objcapset *ocapset, Objcapset *scapset)
1423 1430 {
1424 1431 Alist *oalp, *salp;
1425 1432 elfcap_mask_t omsk, smsk;
1426 1433
1427 1434 /*
1428 1435 * Inspect any platform capabilities. If the object defines platform
1429 1436 * capabilities, then the object will only be loaded for those
1430 1437 * platforms. A symbol capability set that doesn't define the same
1431 1438 * platforms is redundant, and a symbol capability that does not provide
1432 1439 * at least one platform name that matches a platform name in the object
1433 1440 * capabilities will never execute (as the object wouldn't have been
1434 1441 * loaded).
1435 1442 */
1436 1443 oalp = ocapset->oc_plat.cl_val;
1437 1444 salp = scapset->oc_plat.cl_val;
1438 1445 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1439 1446 return (1);
1440 1447
1441 1448 /*
1442 1449 * If the symbol capability set defines platforms, and the object
1443 1450 * doesn't, then the symbol set is more restrictive.
1444 1451 */
1445 1452 if (salp && (oalp == NULL))
1446 1453 return (0);
1447 1454
1448 1455 /*
1449 1456 * Next, inspect any machine name capabilities. If the object defines
1450 1457 * machine name capabilities, then the object will only be loaded for
1451 1458 * those machines. A symbol capability set that doesn't define the same
1452 1459 * machine names is redundant, and a symbol capability that does not
1453 1460 * provide at least one machine name that matches a machine name in the
1454 1461 * object capabilities will never execute (as the object wouldn't have
1455 1462 * been loaded).
1456 1463 */
1457 1464 oalp = ocapset->oc_plat.cl_val;
1458 1465 salp = scapset->oc_plat.cl_val;
1459 1466 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1460 1467 return (1);
1461 1468
1462 1469 /*
1463 1470 * If the symbol capability set defines machine names, and the object
1464 1471 * doesn't, then the symbol set is more restrictive.
1465 1472 */
1466 1473 if (salp && (oalp == NULL))
1467 1474 return (0);
1468 1475
1469 1476 /*
1470 1477 * Next, inspect any hardware capabilities. If the objects hardware
1471 1478 * capabilities are greater than or equal to that of the symbols
1472 1479 * capabilities, then the symbol capability set is redundant. If the
1473 1480 * symbols hardware capabilities are greater that the objects, then the
1474 1481 * symbol set is more restrictive.
1475 1482 *
1476 1483 * Note that this is a somewhat arbitrary definition, as each capability
1477 1484 * bit is independent of the others, and some of the higher order bits
1478 1485 * could be considered to be less important than lower ones. However,
1479 1486 * this is the only reasonable non-subjective definition.
1480 1487 */
1481 1488 omsk = ocapset->oc_hw_2.cm_val;
1482 1489 smsk = scapset->oc_hw_2.cm_val;
1483 1490 if ((omsk > smsk) || (omsk && (omsk == smsk)))
1484 1491 return (1);
1485 1492 if (omsk < smsk)
1486 1493 return (0);
1487 1494
1488 1495 /*
1489 1496 * Finally, inspect the remaining hardware capabilities.
1490 1497 */
1491 1498 omsk = ocapset->oc_hw_1.cm_val;
1492 1499 smsk = scapset->oc_hw_1.cm_val;
1493 1500 if ((omsk > smsk) || (omsk && (omsk == smsk)))
1494 1501 return (1);
1495 1502
1496 1503 return (0);
1497 1504 }
1498 1505
1499 1506 /*
1500 1507 * Capabilities values might have been assigned excluded values. These
1501 1508 * excluded values should be removed before calculating any capabilities
1502 1509 * sections size.
1503 1510 */
1504 1511 static void
1505 1512 capmask_value(Lm_list *lml, Word type, Capmask *capmask, int *title)
1506 1513 {
1507 1514 /*
1508 1515 * First determine whether any bits should be excluded.
1509 1516 */
1510 1517 if ((capmask->cm_val & capmask->cm_exc) == 0)
1511 1518 return;
1512 1519
1513 1520 DBG_CALL(Dbg_cap_post_title(lml, title));
1514 1521
1515 1522 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_CURRENT, type,
1516 1523 capmask->cm_val, ld_targ.t_m.m_mach));
1517 1524 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_EXCLUDE, type,
1518 1525 capmask->cm_exc, ld_targ.t_m.m_mach));
1519 1526
1520 1527 capmask->cm_val &= ~capmask->cm_exc;
1521 1528
1522 1529 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_RESOLVED, type,
1523 1530 capmask->cm_val, ld_targ.t_m.m_mach));
1524 1531 }
1525 1532
1526 1533 static void
1527 1534 capstr_value(Lm_list *lml, Word type, Caplist *caplist, int *title)
1528 1535 {
1529 1536 Aliste idx1, idx2;
1530 1537 char *estr;
1531 1538 Capstr *capstr;
1532 1539 Boolean found = FALSE;
1533 1540
1534 1541 /*
1535 1542 * First determine whether any strings should be excluded.
1536 1543 */
1537 1544 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1538 1545 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1539 1546 if (strcmp(estr, capstr->cs_str) == 0) {
1540 1547 found = TRUE;
1541 1548 break;
1542 1549 }
1543 1550 }
1544 1551 }
1545 1552
1546 1553 if (found == FALSE)
1547 1554 return;
1548 1555
1549 1556 /*
1550 1557 * Traverse the current strings, then delete the excluded strings,
1551 1558 * and finally display the resolved strings.
1552 1559 */
1553 1560 if (DBG_ENABLED) {
1554 1561 Dbg_cap_post_title(lml, title);
1555 1562 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1556 1563 Dbg_cap_ptr_entry(lml, DBG_STATE_CURRENT, type,
1557 1564 capstr->cs_str);
1558 1565 }
1559 1566 }
1560 1567 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1561 1568 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1562 1569 if (strcmp(estr, capstr->cs_str) == 0) {
1563 1570 DBG_CALL(Dbg_cap_ptr_entry(lml,
1564 1571 DBG_STATE_EXCLUDE, type, capstr->cs_str));
1565 1572 alist_delete(caplist->cl_val, &idx2);
1566 1573 break;
1567 1574 }
1568 1575 }
1569 1576 }
1570 1577 if (DBG_ENABLED) {
1571 1578 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1572 1579 Dbg_cap_ptr_entry(lml, DBG_STATE_RESOLVED, type,
1573 1580 capstr->cs_str);
1574 1581 }
1575 1582 }
1576 1583 }
1577 1584
1578 1585 /*
1579 1586 * Build a capabilities section.
1580 1587 */
1581 1588 #define CAP_UPDATE(cap, capndx, tag, val) \
1582 1589 cap->c_tag = tag; \
1583 1590 cap->c_un.c_val = val; \
1584 1591 cap++, capndx++;
1585 1592
1586 1593 static uintptr_t
1587 1594 make_cap(Ofl_desc *ofl, Word shtype, const char *shname, int ident)
1588 1595 {
1589 1596 Shdr *shdr;
1590 1597 Elf_Data *data;
1591 1598 Is_desc *isec;
1592 1599 Cap *cap;
1593 1600 size_t size = 0;
1594 1601 Word capndx = 0;
1595 1602 Str_tbl *strtbl;
1596 1603 Objcapset *ocapset = &ofl->ofl_ocapset;
1597 1604 Aliste idx1;
1598 1605 Capstr *capstr;
1599 1606 int title = 0;
1600 1607
1601 1608 /*
1602 1609 * Determine which string table to use for any CA_SUNW_MACH,
1603 1610 * CA_SUNW_PLAT, or CA_SUNW_ID strings.
1604 1611 */
1605 1612 if (OFL_IS_STATIC_OBJ(ofl))
1606 1613 strtbl = ofl->ofl_strtab;
1607 1614 else
1608 1615 strtbl = ofl->ofl_dynstrtab;
1609 1616
1610 1617 /*
1611 1618 * If symbol capabilities have been requested, but none have been
1612 1619 * created, warn the user. This scenario can occur if none of the
1613 1620 * input relocatable objects defined any object capabilities.
1614 1621 */
1615 1622 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && (ofl->ofl_capsymcnt == 0))
1616 1623 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1617 1624
1618 1625 /*
1619 1626 * If symbol capabilities have been collected, but no symbols are left
1620 1627 * referencing these capabilities, promote the capability groups back
1621 1628 * to an object capability definition.
1622 1629 */
1623 1630 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && ofl->ofl_capsymcnt &&
1624 1631 (ofl->ofl_capfamilies == NULL)) {
1625 1632 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1626 1633 ld_cap_move_symtoobj(ofl);
1627 1634 ofl->ofl_capsymcnt = 0;
1628 1635 ofl->ofl_capgroups = NULL;
1629 1636 ofl->ofl_flags &= ~FLG_OF_OTOSCAP;
1630 1637 }
1631 1638
1632 1639 /*
1633 1640 * Remove any excluded capabilities.
1634 1641 */
1635 1642 capstr_value(ofl->ofl_lml, CA_SUNW_PLAT, &ocapset->oc_plat, &title);
1636 1643 capstr_value(ofl->ofl_lml, CA_SUNW_MACH, &ocapset->oc_mach, &title);
1637 1644 capmask_value(ofl->ofl_lml, CA_SUNW_HW_2, &ocapset->oc_hw_2, &title);
1638 1645 capmask_value(ofl->ofl_lml, CA_SUNW_HW_1, &ocapset->oc_hw_1, &title);
1639 1646 capmask_value(ofl->ofl_lml, CA_SUNW_SF_1, &ocapset->oc_sf_1, &title);
1640 1647
1641 1648 /*
1642 1649 * Determine how many entries are required for any object capabilities.
1643 1650 */
1644 1651 size += alist_nitems(ocapset->oc_plat.cl_val);
1645 1652 size += alist_nitems(ocapset->oc_mach.cl_val);
1646 1653 if (ocapset->oc_hw_2.cm_val)
1647 1654 size++;
1648 1655 if (ocapset->oc_hw_1.cm_val)
1649 1656 size++;
1650 1657 if (ocapset->oc_sf_1.cm_val)
1651 1658 size++;
1652 1659
1653 1660 /*
1654 1661 * Only identify a capabilities group if the group has content. If a
1655 1662 * capabilities identifier exists, and no other capabilities have been
1656 1663 * supplied, remove the identifier. This scenario could exist if a
1657 1664 * user mistakenly defined a lone identifier, or if an identified group
1658 1665 * was overridden so as to clear the existing capabilities and the
1659 1666 * identifier was not also cleared.
1660 1667 */
1661 1668 if (ocapset->oc_id.cs_str) {
1662 1669 if (size)
1663 1670 size++;
1664 1671 else
1665 1672 ocapset->oc_id.cs_str = NULL;
1666 1673 }
1667 1674 if (size)
1668 1675 size++; /* Add CA_SUNW_NULL */
1669 1676
1670 1677 /*
1671 1678 * Determine how many entries are required for any symbol capabilities.
1672 1679 */
1673 1680 if (ofl->ofl_capsymcnt) {
1674 1681 /*
1675 1682 * If there are no object capabilities, a CA_SUNW_NULL entry
1676 1683 * is required before any symbol capabilities.
1677 1684 */
1678 1685 if (size == 0)
1679 1686 size++;
1680 1687 size += ofl->ofl_capsymcnt;
1681 1688 }
1682 1689
1683 1690 if (size == 0)
1684 1691 return (NULL);
1685 1692
1686 1693 if (new_section(ofl, shtype, shname, size, &isec,
1687 1694 &shdr, &data) == S_ERROR)
1688 1695 return (S_ERROR);
1689 1696
1690 1697 if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL)
1691 1698 return (S_ERROR);
1692 1699
1693 1700 cap = (Cap *)data->d_buf;
1694 1701
1695 1702 /*
1696 1703 * Fill in any object capabilities. If there is an identifier, then the
1697 1704 * identifier comes first. The remaining items follow in precedence
1698 1705 * order, although the order isn't important for runtime verification.
1699 1706 */
1700 1707 if (ocapset->oc_id.cs_str) {
1701 1708 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1702 1709 if (st_insert(strtbl, ocapset->oc_id.cs_str) == -1)
1703 1710 return (S_ERROR);
1704 1711 ocapset->oc_id.cs_ndx = capndx;
1705 1712 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1706 1713 }
1707 1714 if (ocapset->oc_plat.cl_val) {
1708 1715 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1709 1716
1710 1717 /*
1711 1718 * Insert any platform name strings in the appropriate string
1712 1719 * table. The capability value can't be filled in yet, as the
1713 1720 * final offset of the strings isn't known until later.
1714 1721 */
1715 1722 for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx1, capstr)) {
1716 1723 if (st_insert(strtbl, capstr->cs_str) == -1)
1717 1724 return (S_ERROR);
1718 1725 capstr->cs_ndx = capndx;
1719 1726 CAP_UPDATE(cap, capndx, CA_SUNW_PLAT, 0);
1720 1727 }
1721 1728 }
1722 1729 if (ocapset->oc_mach.cl_val) {
1723 1730 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1724 1731
1725 1732 /*
1726 1733 * Insert the machine name strings in the appropriate string
1727 1734 * table. The capability value can't be filled in yet, as the
1728 1735 * final offset of the strings isn't known until later.
1729 1736 */
1730 1737 for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx1, capstr)) {
1731 1738 if (st_insert(strtbl, capstr->cs_str) == -1)
1732 1739 return (S_ERROR);
1733 1740 capstr->cs_ndx = capndx;
1734 1741 CAP_UPDATE(cap, capndx, CA_SUNW_MACH, 0);
1735 1742 }
1736 1743 }
1737 1744 if (ocapset->oc_hw_2.cm_val) {
1738 1745 ofl->ofl_flags |= FLG_OF_PTCAP;
1739 1746 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2, ocapset->oc_hw_2.cm_val);
1740 1747 }
1741 1748 if (ocapset->oc_hw_1.cm_val) {
1742 1749 ofl->ofl_flags |= FLG_OF_PTCAP;
1743 1750 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1, ocapset->oc_hw_1.cm_val);
1744 1751 }
1745 1752 if (ocapset->oc_sf_1.cm_val) {
1746 1753 ofl->ofl_flags |= FLG_OF_PTCAP;
1747 1754 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1, ocapset->oc_sf_1.cm_val);
1748 1755 }
1749 1756 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1750 1757
1751 1758 /*
1752 1759 * Fill in any symbol capabilities.
1753 1760 */
1754 1761 if (ofl->ofl_capgroups) {
1755 1762 Cap_group *cgp;
1756 1763
1757 1764 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
1758 1765 Objcapset *scapset = &cgp->cg_set;
1759 1766 Aliste idx2;
1760 1767 Is_desc *isp;
1761 1768
1762 1769 cgp->cg_ndx = capndx;
1763 1770
1764 1771 if (scapset->oc_id.cs_str) {
1765 1772 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1766 1773 /*
1767 1774 * Insert the identifier string in the
1768 1775 * appropriate string table. The capability
1769 1776 * value can't be filled in yet, as the final
1770 1777 * offset of the string isn't known until later.
1771 1778 */
1772 1779 if (st_insert(strtbl,
1773 1780 scapset->oc_id.cs_str) == -1)
1774 1781 return (S_ERROR);
1775 1782 scapset->oc_id.cs_ndx = capndx;
1776 1783 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1777 1784 }
1778 1785
1779 1786 if (scapset->oc_plat.cl_val) {
1780 1787 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1781 1788
1782 1789 /*
1783 1790 * Insert the platform name string in the
1784 1791 * appropriate string table. The capability
1785 1792 * value can't be filled in yet, as the final
1786 1793 * offset of the string isn't known until later.
1787 1794 */
1788 1795 for (ALIST_TRAVERSE(scapset->oc_plat.cl_val,
1789 1796 idx2, capstr)) {
1790 1797 if (st_insert(strtbl,
1791 1798 capstr->cs_str) == -1)
1792 1799 return (S_ERROR);
1793 1800 capstr->cs_ndx = capndx;
1794 1801 CAP_UPDATE(cap, capndx,
1795 1802 CA_SUNW_PLAT, 0);
1796 1803 }
1797 1804 }
1798 1805 if (scapset->oc_mach.cl_val) {
1799 1806 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1800 1807
1801 1808 /*
1802 1809 * Insert the machine name string in the
1803 1810 * appropriate string table. The capability
1804 1811 * value can't be filled in yet, as the final
1805 1812 * offset of the string isn't known until later.
1806 1813 */
1807 1814 for (ALIST_TRAVERSE(scapset->oc_mach.cl_val,
1808 1815 idx2, capstr)) {
1809 1816 if (st_insert(strtbl,
1810 1817 capstr->cs_str) == -1)
1811 1818 return (S_ERROR);
1812 1819 capstr->cs_ndx = capndx;
1813 1820 CAP_UPDATE(cap, capndx,
1814 1821 CA_SUNW_MACH, 0);
1815 1822 }
1816 1823 }
1817 1824 if (scapset->oc_hw_2.cm_val) {
1818 1825 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2,
1819 1826 scapset->oc_hw_2.cm_val);
1820 1827 }
1821 1828 if (scapset->oc_hw_1.cm_val) {
1822 1829 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1,
1823 1830 scapset->oc_hw_1.cm_val);
1824 1831 }
1825 1832 if (scapset->oc_sf_1.cm_val) {
1826 1833 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1,
1827 1834 scapset->oc_sf_1.cm_val);
1828 1835 }
1829 1836 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1830 1837
1831 1838 /*
1832 1839 * If any object capabilities are available, determine
1833 1840 * whether these symbol capabilities are less
1834 1841 * restrictive, and hence redundant.
1835 1842 */
1836 1843 if (((ofl->ofl_flags & FLG_OF_PTCAP) == 0) ||
1837 1844 (is_cap_redundant(ocapset, scapset) == 0))
1838 1845 continue;
1839 1846
1840 1847 /*
1841 1848 * Indicate any files that provide redundant symbol
1842 1849 * capabilities.
1843 1850 */
1844 1851 for (APLIST_TRAVERSE(cgp->cg_secs, idx2, isp)) {
1845 1852 ld_eprintf(ofl, ERR_WARNING,
1846 1853 MSG_INTL(MSG_CAP_REDUNDANT),
1847 1854 isp->is_file->ifl_name,
1848 1855 EC_WORD(isp->is_scnndx), isp->is_name);
1849 1856 }
1850 1857 }
1851 1858 }
1852 1859
1853 1860 /*
1854 1861 * If capabilities strings are required, the sh_info field of the
1855 1862 * section header will be set to the associated string table.
1856 1863 */
1857 1864 if (ofl->ofl_flags & FLG_OF_CAPSTRS)
1858 1865 shdr->sh_flags |= SHF_INFO_LINK;
1859 1866
1860 1867 /*
1861 1868 * Place these capabilities in the output file.
1862 1869 */
1863 1870 if ((ofl->ofl_oscap = ld_place_section(ofl, isec,
1864 1871 NULL, ident, NULL)) == (Os_desc *)S_ERROR)
1865 1872 return (S_ERROR);
1866 1873
1867 1874 /*
1868 1875 * If symbol capabilities are required, then a .SUNW_capinfo section is
1869 1876 * also created. This table will eventually be sized to match the
1870 1877 * associated symbol table.
1871 1878 */
1872 1879 if (ofl->ofl_capfamilies) {
1873 1880 if ((ofl->ofl_oscapinfo = make_sym_sec(ofl,
1874 1881 MSG_ORIG(MSG_SCN_SUNWCAPINFO), SHT_SUNW_capinfo,
1875 1882 ld_targ.t_id.id_capinfo)) == (Os_desc *)S_ERROR)
1876 1883 return (S_ERROR);
1877 1884
1878 1885 /*
1879 1886 * If we're generating a dynamic object, capabilities family
1880 1887 * members are maintained in a .SUNW_capchain section.
1881 1888 */
1882 1889 if (ofl->ofl_capchaincnt &&
1883 1890 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1884 1891 if (new_section(ofl, SHT_SUNW_capchain,
1885 1892 MSG_ORIG(MSG_SCN_SUNWCAPCHAIN),
1886 1893 ofl->ofl_capchaincnt, &isec, &shdr,
1887 1894 &data) == S_ERROR)
1888 1895 return (S_ERROR);
1889 1896
1890 1897 ofl->ofl_oscapchain = ld_place_section(ofl, isec,
1891 1898 NULL, ld_targ.t_id.id_capchain, NULL);
1892 1899 if (ofl->ofl_oscapchain == (Os_desc *)S_ERROR)
1893 1900 return (S_ERROR);
1894 1901
1895 1902 }
1896 1903 }
1897 1904 return (1);
1898 1905 }
1899 1906 #undef CAP_UPDATE
1900 1907
1901 1908 /*
1902 1909 * Build the PLT section and its associated relocation entries.
1903 1910 */
1904 1911 static uintptr_t
1905 1912 make_plt(Ofl_desc *ofl)
1906 1913 {
1907 1914 Shdr *shdr;
1908 1915 Elf_Data *data;
1909 1916 Is_desc *isec;
1910 1917 size_t size = ld_targ.t_m.m_plt_reservsz +
1911 1918 (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1912 1919 ld_targ.t_m.m_plt_entsize);
1913 1920 size_t rsize = (size_t)ofl->ofl_relocpltsz;
1914 1921
1915 1922 /*
1916 1923 * On sparc, account for the NOP at the end of the plt.
1917 1924 */
1918 1925 if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1919 1926 size += sizeof (Word);
1920 1927
1921 1928 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1922 1929 &isec, &shdr, &data) == S_ERROR)
1923 1930 return (S_ERROR);
1924 1931
1925 1932 data->d_size = size;
1926 1933 data->d_align = ld_targ.t_m.m_plt_align;
1927 1934
1928 1935 shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1929 1936 shdr->sh_size = (Xword)size;
1930 1937 shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1931 1938 shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1932 1939
1933 1940 ofl->ofl_osplt = ld_place_section(ofl, isec, NULL,
1934 1941 ld_targ.t_id.id_plt, NULL);
1935 1942 if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1936 1943 return (S_ERROR);
1937 1944
1938 1945 ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1939 1946
1940 1947 return (1);
1941 1948 }
1942 1949
1943 1950 /*
1944 1951 * Make the hash table. Only built for dynamic executables and shared
1945 1952 * libraries, and provides hashed lookup into the global symbol table
1946 1953 * (.dynsym) for the run-time linker to resolve symbol lookups.
1947 1954 */
1948 1955 static uintptr_t
1949 1956 make_hash(Ofl_desc *ofl)
1950 1957 {
1951 1958 Shdr *shdr;
1952 1959 Elf_Data *data;
1953 1960 Is_desc *isec;
1954 1961 size_t size;
1955 1962 Word nsyms = ofl->ofl_globcnt;
1956 1963 size_t cnt;
1957 1964
1958 1965 /*
1959 1966 * Allocate section header structures. We set entcnt to 0
1960 1967 * because it's going to change after we place this section.
1961 1968 */
1962 1969 if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1963 1970 &isec, &shdr, &data) == S_ERROR)
1964 1971 return (S_ERROR);
1965 1972
1966 1973 /*
1967 1974 * Place the section first since it will affect the local symbol
1968 1975 * count.
1969 1976 */
1970 1977 ofl->ofl_oshash =
1971 1978 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_hash, NULL);
1972 1979 if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
1973 1980 return (S_ERROR);
1974 1981
1975 1982 /*
1976 1983 * Calculate the number of output hash buckets.
1977 1984 */
1978 1985 ofl->ofl_hashbkts = findprime(nsyms);
1979 1986
1980 1987 /*
1981 1988 * The size of the hash table is determined by
1982 1989 *
1983 1990 * i. the initial nbucket and nchain entries (2)
1984 1991 * ii. the number of buckets (calculated above)
1985 1992 * iii. the number of chains (this is based on the number of
1986 1993 * symbols in the .dynsym array).
1987 1994 */
1988 1995 cnt = 2 + ofl->ofl_hashbkts + DYNSYM_ALL_CNT(ofl);
1989 1996 size = cnt * shdr->sh_entsize;
1990 1997
1991 1998 /*
1992 1999 * Finalize the section header and data buffer initialization.
1993 2000 */
1994 2001 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
1995 2002 return (S_ERROR);
1996 2003 data->d_size = size;
1997 2004 shdr->sh_size = (Xword)size;
1998 2005
1999 2006 return (1);
2000 2007 }
2001 2008
2002 2009 /*
2003 2010 * Generate the standard symbol table. Contains all locals and globals,
2004 2011 * and resides in a non-allocatable section (ie. it can be stripped).
2005 2012 */
2006 2013 static uintptr_t
2007 2014 make_symtab(Ofl_desc *ofl)
2008 2015 {
2009 2016 Shdr *shdr;
2010 2017 Elf_Data *data;
2011 2018 Is_desc *isec;
2012 2019 Is_desc *xisec = 0;
2013 2020 size_t size;
2014 2021 Word symcnt;
2015 2022
2016 2023 /*
2017 2024 * Create the section headers. Note that we supply an ent_cnt
2018 2025 * of 0. We won't know the count until the section has been placed.
2019 2026 */
2020 2027 if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
2021 2028 &isec, &shdr, &data) == S_ERROR)
2022 2029 return (S_ERROR);
2023 2030
2024 2031 /*
2025 2032 * Place the section first since it will affect the local symbol
2026 2033 * count.
2027 2034 */
2028 2035 if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, NULL,
2029 2036 ld_targ.t_id.id_symtab, NULL)) == (Os_desc *)S_ERROR)
2030 2037 return (S_ERROR);
2031 2038
2032 2039 /*
2033 2040 * At this point we've created all but the 'shstrtab' section.
2034 2041 * Determine if we have to use 'Extended Sections'. If so - then
2035 2042 * also create a SHT_SYMTAB_SHNDX section.
2036 2043 */
2037 2044 if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
2038 2045 Shdr *xshdr;
2039 2046 Elf_Data *xdata;
2040 2047
2041 2048 if (new_section(ofl, SHT_SYMTAB_SHNDX,
2042 2049 MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
2043 2050 &xshdr, &xdata) == S_ERROR)
2044 2051 return (S_ERROR);
2045 2052
2046 2053 if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, NULL,
2047 2054 ld_targ.t_id.id_symtab_ndx, NULL)) == (Os_desc *)S_ERROR)
2048 2055 return (S_ERROR);
2049 2056 }
2050 2057
2051 2058 /*
2052 2059 * Calculated number of symbols, which need to be augmented by
2053 2060 * the (yet to be created) .shstrtab entry.
2054 2061 */
2055 2062 symcnt = (size_t)(1 + SYMTAB_ALL_CNT(ofl));
2056 2063 size = symcnt * shdr->sh_entsize;
2057 2064
2058 2065 /*
2059 2066 * Finalize the section header and data buffer initialization.
2060 2067 */
2061 2068 data->d_size = size;
2062 2069 shdr->sh_size = (Xword)size;
2063 2070
2064 2071 /*
2065 2072 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
2066 2073 */
2067 2074 if (xisec) {
2068 2075 size_t xsize = symcnt * sizeof (Word);
2069 2076
2070 2077 xisec->is_indata->d_size = xsize;
2071 2078 xisec->is_shdr->sh_size = (Xword)xsize;
2072 2079 }
2073 2080
2074 2081 return (1);
2075 2082 }
2076 2083
2077 2084 /*
2078 2085 * Build a dynamic symbol table. These tables reside in the text
2079 2086 * segment of a dynamic executable or shared library.
2080 2087 *
2081 2088 * .SUNW_ldynsym contains local function symbols
2082 2089 * .dynsym contains only globals symbols
2083 2090 *
2084 2091 * The two tables are created adjacent to each other, with .SUNW_ldynsym
2085 2092 * coming first.
2086 2093 */
2087 2094 static uintptr_t
2088 2095 make_dynsym(Ofl_desc *ofl)
2089 2096 {
2090 2097 Shdr *shdr, *lshdr;
2091 2098 Elf_Data *data, *ldata;
2092 2099 Is_desc *isec, *lisec;
2093 2100 size_t size;
2094 2101 Xword cnt;
2095 2102 int allow_ldynsym;
2096 2103
2097 2104 /*
2098 2105 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
2099 2106 * when it is allowed by the file type, even if the resulting
2100 2107 * table only ends up with a single STT_FILE in it. There are
2101 2108 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
2102 2109 * entry in the .dynamic section, which is something we would
2103 2110 * like to encourage, and (2) Without it, we cannot generate
2104 2111 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
2105 2112 * value to DTrace.
2106 2113 *
2107 2114 * In practice, it is extremely rare for an object not to have
2108 2115 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
2109 2116 * doing it anyway.
2110 2117 */
2111 2118 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
2112 2119
2113 2120 /*
2114 2121 * Create the section headers. Note that we supply an ent_cnt
2115 2122 * of 0. We won't know the count until the section has been placed.
2116 2123 */
2117 2124 if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
2118 2125 MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
2119 2126 return (S_ERROR);
2120 2127
2121 2128 if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
2122 2129 &isec, &shdr, &data) == S_ERROR)
2123 2130 return (S_ERROR);
2124 2131
2125 2132 /*
2126 2133 * Place the section(s) first since it will affect the local symbol
2127 2134 * count.
2128 2135 */
2129 2136 if (allow_ldynsym &&
2130 2137 ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, NULL,
2131 2138 ld_targ.t_id.id_ldynsym, NULL)) == (Os_desc *)S_ERROR))
2132 2139 return (S_ERROR);
2133 2140 ofl->ofl_osdynsym =
2134 2141 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynsym, NULL);
2135 2142 if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
2136 2143 return (S_ERROR);
2137 2144
2138 2145 cnt = DYNSYM_ALL_CNT(ofl);
2139 2146 size = (size_t)cnt * shdr->sh_entsize;
2140 2147
2141 2148 /*
2142 2149 * Finalize the section header and data buffer initialization.
2143 2150 */
2144 2151 data->d_size = size;
2145 2152 shdr->sh_size = (Xword)size;
2146 2153
2147 2154 /*
2148 2155 * An ldynsym contains local function symbols. It is not
2149 2156 * used for linking, but if present, serves to allow better
2150 2157 * stack traces to be generated in contexts where the symtab
2151 2158 * is not available. (dladdr(), or stripped executable/library files).
2152 2159 */
2153 2160 if (allow_ldynsym) {
2154 2161 cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
2155 2162 size = (size_t)cnt * shdr->sh_entsize;
2156 2163
2157 2164 ldata->d_size = size;
2158 2165 lshdr->sh_size = (Xword)size;
2159 2166 }
2160 2167
2161 2168 return (1);
2162 2169 }
2163 2170
2164 2171 /*
2165 2172 * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
2166 2173 * index sections for the .SUNW_ldynsym/.dynsym pair that present data
2167 2174 * and function symbols sorted by address.
2168 2175 */
2169 2176 static uintptr_t
2170 2177 make_dynsort(Ofl_desc *ofl)
2171 2178 {
2172 2179 Shdr *shdr;
2173 2180 Elf_Data *data;
2174 2181 Is_desc *isec;
2175 2182
2176 2183 /* Only do it if the .SUNW_ldynsym section is present */
2177 2184 if (!OFL_ALLOW_LDYNSYM(ofl))
2178 2185 return (1);
2179 2186
2180 2187 /* .SUNW_dynsymsort */
2181 2188 if (ofl->ofl_dynsymsortcnt > 0) {
2182 2189 if (new_section(ofl, SHT_SUNW_symsort,
2183 2190 MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
2184 2191 &isec, &shdr, &data) == S_ERROR)
2185 2192 return (S_ERROR);
2186 2193
2187 2194 if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, NULL,
2188 2195 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2189 2196 return (S_ERROR);
2190 2197 }
2191 2198
2192 2199 /* .SUNW_dyntlssort */
2193 2200 if (ofl->ofl_dyntlssortcnt > 0) {
2194 2201 if (new_section(ofl, SHT_SUNW_tlssort,
2195 2202 MSG_ORIG(MSG_SCN_DYNTLSSORT),
2196 2203 ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
2197 2204 return (S_ERROR);
2198 2205
2199 2206 if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec, NULL,
2200 2207 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2201 2208 return (S_ERROR);
2202 2209 }
2203 2210
2204 2211 return (1);
2205 2212 }
2206 2213
2207 2214 /*
2208 2215 * Helper routine for make_dynsym_shndx. Builds a
2209 2216 * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
2210 2217 * which one it is.
2211 2218 */
2212 2219 static uintptr_t
2213 2220 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
2214 2221 Os_desc **ret_os)
2215 2222 {
2216 2223 Is_desc *isec;
2217 2224 Is_desc *dynsymisp;
2218 2225 Shdr *shdr, *dynshdr;
2219 2226 Elf_Data *data;
2220 2227
2221 2228 dynsymisp = ld_os_first_isdesc(symtab);
2222 2229 dynshdr = dynsymisp->is_shdr;
2223 2230
2224 2231 if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
2225 2232 (dynshdr->sh_size / dynshdr->sh_entsize),
2226 2233 &isec, &shdr, &data) == S_ERROR)
2227 2234 return (S_ERROR);
2228 2235
2229 2236 if ((*ret_os = ld_place_section(ofl, isec, NULL,
2230 2237 ld_targ.t_id.id_dynsym_ndx, NULL)) == (Os_desc *)S_ERROR)
2231 2238 return (S_ERROR);
2232 2239
2233 2240 assert(*ret_os);
2234 2241
2235 2242 return (1);
2236 2243 }
2237 2244
2238 2245 /*
2239 2246 * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
2240 2247 */
2241 2248 static uintptr_t
2242 2249 make_dynsym_shndx(Ofl_desc *ofl)
2243 2250 {
2244 2251 /*
2245 2252 * If there is a .SUNW_ldynsym, generate a section for its extended
2246 2253 * index section as well.
2247 2254 */
2248 2255 if (OFL_ALLOW_LDYNSYM(ofl)) {
2249 2256 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
2250 2257 ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
2251 2258 return (S_ERROR);
2252 2259 }
2253 2260
2254 2261 /* The Generate a section for the dynsym */
2255 2262 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
2256 2263 ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
2257 2264 return (S_ERROR);
2258 2265
2259 2266 return (1);
2260 2267 }
2261 2268
2262 2269
2263 2270 /*
2264 2271 * Build a string table for the section headers.
2265 2272 */
2266 2273 static uintptr_t
2267 2274 make_shstrtab(Ofl_desc *ofl)
2268 2275 {
2269 2276 Shdr *shdr;
2270 2277 Elf_Data *data;
2271 2278 Is_desc *isec;
2272 2279 size_t size;
2273 2280
2274 2281 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
2275 2282 0, &isec, &shdr, &data) == S_ERROR)
2276 2283 return (S_ERROR);
2277 2284
2278 2285 /*
2279 2286 * Place the section first, as it may effect the number of section
2280 2287 * headers to account for.
2281 2288 */
2282 2289 ofl->ofl_osshstrtab =
2283 2290 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_note, NULL);
2284 2291 if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
2285 2292 return (S_ERROR);
2286 2293
2287 2294 size = st_getstrtab_sz(ofl->ofl_shdrsttab);
2288 2295 assert(size > 0);
2289 2296
2290 2297 data->d_size = size;
2291 2298 shdr->sh_size = (Xword)size;
2292 2299
2293 2300 return (1);
2294 2301 }
2295 2302
2296 2303 /*
2297 2304 * Build a string section for the standard symbol table.
2298 2305 */
2299 2306 static uintptr_t
2300 2307 make_strtab(Ofl_desc *ofl)
2301 2308 {
2302 2309 Shdr *shdr;
2303 2310 Elf_Data *data;
2304 2311 Is_desc *isec;
2305 2312 size_t size;
2306 2313
2307 2314 /*
2308 2315 * This string table consists of all the global and local symbols.
2309 2316 * Account for null bytes at end of the file name and the beginning
2310 2317 * of section.
2311 2318 */
2312 2319 if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
2313 2320 return (S_ERROR);
2314 2321
2315 2322 size = st_getstrtab_sz(ofl->ofl_strtab);
2316 2323 assert(size > 0);
2317 2324
2318 2325 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
2319 2326 0, &isec, &shdr, &data) == S_ERROR)
2320 2327 return (S_ERROR);
2321 2328
2322 2329 /* Set the size of the data area */
2323 2330 data->d_size = size;
2324 2331 shdr->sh_size = (Xword)size;
2325 2332
2326 2333 ofl->ofl_osstrtab =
2327 2334 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_strtab, NULL);
2328 2335 return ((uintptr_t)ofl->ofl_osstrtab);
2329 2336 }
2330 2337
2331 2338 /*
2332 2339 * Build a string table for the dynamic symbol table.
2333 2340 */
2334 2341 static uintptr_t
2335 2342 make_dynstr(Ofl_desc *ofl)
2336 2343 {
2337 2344 Shdr *shdr;
2338 2345 Elf_Data *data;
2339 2346 Is_desc *isec;
2340 2347 size_t size;
2341 2348
2342 2349 /*
2343 2350 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
2344 2351 * symbol that precedes the scope reduced global symbols.
2345 2352 */
2346 2353 if (OFL_ALLOW_LDYNSYM(ofl)) {
2347 2354 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
2348 2355 return (S_ERROR);
2349 2356 ofl->ofl_dynscopecnt++;
2350 2357 }
2351 2358
2352 2359 /*
2353 2360 * Account for any local, named register symbols. These locals are
2354 2361 * required for reference from DT_REGISTER .dynamic entries.
2355 2362 */
2356 2363 if (ofl->ofl_regsyms) {
2357 2364 int ndx;
2358 2365
2359 2366 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2360 2367 Sym_desc *sdp;
2361 2368
2362 2369 if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
2363 2370 continue;
2364 2371
2365 2372 if (!SYM_IS_HIDDEN(sdp) &&
2366 2373 (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
2367 2374 continue;
2368 2375
2369 2376 if (sdp->sd_sym->st_name == NULL)
2370 2377 continue;
2371 2378
2372 2379 if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
2373 2380 return (S_ERROR);
2374 2381 }
2375 2382 }
2376 2383
2377 2384 /*
2378 2385 * Reserve entries for any per-symbol auxiliary/filter strings.
2379 2386 */
2380 2387 if (ofl->ofl_dtsfltrs != NULL) {
2381 2388 Dfltr_desc *dftp;
2382 2389 Aliste idx;
2383 2390
2384 2391 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
2385 2392 if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
2386 2393 return (S_ERROR);
2387 2394 }
2388 2395
2389 2396 size = st_getstrtab_sz(ofl->ofl_dynstrtab);
2390 2397 assert(size > 0);
2391 2398
2392 2399 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
2393 2400 0, &isec, &shdr, &data) == S_ERROR)
2394 2401 return (S_ERROR);
2395 2402
2396 2403 /* Make it allocable if necessary */
2397 2404 if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2398 2405 shdr->sh_flags |= SHF_ALLOC;
2399 2406
2400 2407 /* Set the size of the data area */
2401 2408 data->d_size = size + DYNSTR_EXTRA_PAD;
2402 2409
2403 2410 shdr->sh_size = (Xword)size;
2404 2411
2405 2412 ofl->ofl_osdynstr =
2406 2413 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynstr, NULL);
2407 2414 return ((uintptr_t)ofl->ofl_osdynstr);
2408 2415 }
2409 2416
2410 2417 /*
2411 2418 * Generate an output relocation section which will contain the relocation
2412 2419 * information to be applied to the `osp' section.
2413 2420 *
2414 2421 * If (osp == NULL) then we are creating the coalesced relocation section
2415 2422 * for an executable and/or a shared object.
2416 2423 */
2417 2424 static uintptr_t
2418 2425 make_reloc(Ofl_desc *ofl, Os_desc *osp)
2419 2426 {
2420 2427 Shdr *shdr;
2421 2428 Elf_Data *data;
2422 2429 Is_desc *isec;
2423 2430 size_t size;
2424 2431 Xword sh_flags;
2425 2432 char *sectname;
2426 2433 Os_desc *rosp;
2427 2434 Word relsize;
2428 2435 const char *rel_prefix;
2429 2436
2430 2437 /* LINTED */
2431 2438 if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
2432 2439 /* REL */
2433 2440 relsize = sizeof (Rel);
2434 2441 rel_prefix = MSG_ORIG(MSG_SCN_REL);
2435 2442 } else {
2436 2443 /* RELA */
2437 2444 relsize = sizeof (Rela);
2438 2445 rel_prefix = MSG_ORIG(MSG_SCN_RELA);
2439 2446 }
2440 2447
2441 2448 if (osp) {
2442 2449 size = osp->os_szoutrels;
2443 2450 sh_flags = osp->os_shdr->sh_flags;
2444 2451 if ((sectname = libld_malloc(strlen(rel_prefix) +
2445 2452 strlen(osp->os_name) + 1)) == 0)
2446 2453 return (S_ERROR);
2447 2454 (void) strcpy(sectname, rel_prefix);
2448 2455 (void) strcat(sectname, osp->os_name);
2449 2456 } else if (ofl->ofl_flags & FLG_OF_COMREL) {
2450 2457 size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
2451 2458 sh_flags = SHF_ALLOC;
2452 2459 sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
2453 2460 } else {
2454 2461 size = ofl->ofl_relocrelsz;
2455 2462 sh_flags = SHF_ALLOC;
2456 2463 sectname = (char *)rel_prefix;
2457 2464 }
2458 2465
2459 2466 /*
2460 2467 * Keep track of total size of 'output relocations' (to be stored
2461 2468 * in .dynamic)
2462 2469 */
2463 2470 /* LINTED */
2464 2471 ofl->ofl_relocsz += (Xword)size;
2465 2472
2466 2473 if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
2467 2474 &shdr, &data) == S_ERROR)
2468 2475 return (S_ERROR);
2469 2476
2470 2477 data->d_size = size;
2471 2478
2472 2479 shdr->sh_size = (Xword)size;
2473 2480 if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
2474 2481 shdr->sh_flags = SHF_ALLOC;
2475 2482
2476 2483 if (osp) {
2477 2484 /*
2478 2485 * The sh_info field of the SHT_REL* sections points to the
2479 2486 * section the relocations are to be applied to.
2480 2487 */
2481 2488 shdr->sh_flags |= SHF_INFO_LINK;
2482 2489 }
2483 2490
2484 2491 rosp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_rel, NULL);
2485 2492 if (rosp == (Os_desc *)S_ERROR)
2486 2493 return (S_ERROR);
2487 2494
2488 2495 /*
2489 2496 * Associate this relocation section to the section its going to
2490 2497 * relocate.
2491 2498 */
2492 2499 if (osp) {
2493 2500 Aliste idx;
2494 2501 Is_desc *risp;
2495 2502
2496 2503 /*
2497 2504 * This is used primarily so that we can update
2498 2505 * SHT_GROUP[sect_no] entries to point to the
2499 2506 * created output relocation sections.
2500 2507 */
2501 2508 for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) {
2502 2509 risp->is_osdesc = rosp;
2503 2510
2504 2511 /*
2505 2512 * If the input relocation section had the SHF_GROUP
2506 2513 * flag set - propagate it to the output relocation
2507 2514 * section.
2508 2515 */
2509 2516 if (risp->is_shdr->sh_flags & SHF_GROUP) {
2510 2517 rosp->os_shdr->sh_flags |= SHF_GROUP;
2511 2518 break;
2512 2519 }
2513 2520 }
2514 2521 osp->os_relosdesc = rosp;
2515 2522 } else
2516 2523 ofl->ofl_osrel = rosp;
2517 2524
2518 2525 /*
2519 2526 * If this is the first relocation section we've encountered save it
2520 2527 * so that the .dynamic entry can be initialized accordingly.
2521 2528 */
2522 2529 if (ofl->ofl_osrelhead == (Os_desc *)0)
2523 2530 ofl->ofl_osrelhead = rosp;
2524 2531
2525 2532 return (1);
2526 2533 }
2527 2534
2528 2535 /*
2529 2536 * Generate version needed section.
2530 2537 */
2531 2538 static uintptr_t
2532 2539 make_verneed(Ofl_desc *ofl)
2533 2540 {
2534 2541 Shdr *shdr;
2535 2542 Elf_Data *data;
2536 2543 Is_desc *isec;
2537 2544
2538 2545 /*
2539 2546 * verneed sections do not have a constant element size, so the
2540 2547 * value of ent_cnt specified here (0) is meaningless.
2541 2548 */
2542 2549 if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
2543 2550 0, &isec, &shdr, &data) == S_ERROR)
2544 2551 return (S_ERROR);
2545 2552
2546 2553 /* During version processing we calculated the total size. */
2547 2554 data->d_size = ofl->ofl_verneedsz;
2548 2555 shdr->sh_size = (Xword)ofl->ofl_verneedsz;
2549 2556
2550 2557 ofl->ofl_osverneed =
2551 2558 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2552 2559 return ((uintptr_t)ofl->ofl_osverneed);
2553 2560 }
2554 2561
2555 2562 /*
2556 2563 * Generate a version definition section.
2557 2564 *
2558 2565 * o the SHT_SUNW_verdef section defines the versions that exist within this
2559 2566 * image.
2560 2567 */
2561 2568 static uintptr_t
2562 2569 make_verdef(Ofl_desc *ofl)
2563 2570 {
2564 2571 Shdr *shdr;
2565 2572 Elf_Data *data;
2566 2573 Is_desc *isec;
2567 2574 Ver_desc *vdp;
2568 2575 Str_tbl *strtab;
2569 2576
2570 2577 /*
2571 2578 * Reserve a string table entry for the base version dependency (other
2572 2579 * dependencies have symbol representations, which will already be
2573 2580 * accounted for during symbol processing).
2574 2581 */
2575 2582 vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0];
2576 2583
2577 2584 if (OFL_IS_STATIC_OBJ(ofl))
2578 2585 strtab = ofl->ofl_strtab;
2579 2586 else
2580 2587 strtab = ofl->ofl_dynstrtab;
2581 2588
2582 2589 if (st_insert(strtab, vdp->vd_name) == -1)
2583 2590 return (S_ERROR);
2584 2591
2585 2592 /*
2586 2593 * verdef sections do not have a constant element size, so the
2587 2594 * value of ent_cnt specified here (0) is meaningless.
2588 2595 */
2589 2596 if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2590 2597 0, &isec, &shdr, &data) == S_ERROR)
2591 2598 return (S_ERROR);
2592 2599
2593 2600 /* During version processing we calculated the total size. */
2594 2601 data->d_size = ofl->ofl_verdefsz;
2595 2602 shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2596 2603
2597 2604 ofl->ofl_osverdef =
2598 2605 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2599 2606 return ((uintptr_t)ofl->ofl_osverdef);
2600 2607 }
2601 2608
2602 2609 /*
2603 2610 * This routine is called when -z nopartial is in effect.
2604 2611 */
2605 2612 uintptr_t
2606 2613 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align)
2607 2614 {
2608 2615 Shdr *shdr;
2609 2616 Elf_Data *data;
2610 2617 Is_desc *isec;
2611 2618 Os_desc *osp;
2612 2619
2613 2620 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2614 2621 &isec, &shdr, &data) == S_ERROR)
2615 2622 return (S_ERROR);
2616 2623
2617 2624 shdr->sh_flags |= SHF_WRITE;
2618 2625 data->d_size = size;
2619 2626 shdr->sh_size = (Xword)size;
2620 2627 if (align != 0) {
2621 2628 data->d_align = align;
2622 2629 shdr->sh_addralign = align;
2623 2630 }
2624 2631
2625 2632 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2626 2633 return (S_ERROR);
2627 2634
2628 2635 /*
2629 2636 * Retain handle to this .data input section. Variables using move
2630 2637 * sections (partial initialization) will be redirected here when
2631 2638 * such global references are added and '-z nopartial' is in effect.
2632 2639 */
2633 2640 ofl->ofl_isparexpn = isec;
2634 2641 osp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_data, NULL);
2635 2642 if (osp == (Os_desc *)S_ERROR)
2636 2643 return (S_ERROR);
2637 2644
2638 2645 if (!(osp->os_flags & FLG_OS_OUTREL)) {
2639 2646 ofl->ofl_dynshdrcnt++;
2640 2647 osp->os_flags |= FLG_OS_OUTREL;
2641 2648 }
2642 2649 return (1);
2643 2650 }
2644 2651
2645 2652 /*
2646 2653 * Make .sunwmove section
2647 2654 */
2648 2655 uintptr_t
2649 2656 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2650 2657 {
2651 2658 Shdr *shdr;
2652 2659 Elf_Data *data;
2653 2660 Is_desc *isec;
2654 2661 Aliste idx;
2655 2662 Sym_desc *sdp;
2656 2663 int cnt = 1;
2657 2664
2658 2665
2659 2666 if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2660 2667 mv_nums, &isec, &shdr, &data) == S_ERROR)
2661 2668 return (S_ERROR);
2662 2669
2663 2670 if ((data->d_buf = libld_calloc(data->d_size, 1)) == NULL)
2664 2671 return (S_ERROR);
2665 2672
2666 2673 /*
2667 2674 * Copy move entries
2668 2675 */
2669 2676 for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx, sdp)) {
2670 2677 Aliste idx2;
2671 2678 Mv_desc *mdp;
2672 2679
2673 2680 if (sdp->sd_flags & FLG_SY_PAREXPN)
2674 2681 continue;
2675 2682
2676 2683 for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp))
2677 2684 mdp->md_oidx = cnt++;
2678 2685 }
2679 2686
2680 2687 if ((ofl->ofl_osmove = ld_place_section(ofl, isec, NULL, 0, NULL)) ==
2681 2688 (Os_desc *)S_ERROR)
2682 2689 return (S_ERROR);
2683 2690
2684 2691 return (1);
2685 2692 }
2686 2693
2687 2694 /*
2688 2695 * Given a relocation descriptor that references a string table
2689 2696 * input section, locate the string referenced and return a pointer
2690 2697 * to it.
2691 2698 */
2692 2699 static const char *
2693 2700 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2694 2701 {
2695 2702 Sym_desc *sdp = rsp->rel_sym;
2696 2703 Xword str_off;
2697 2704
2698 2705 /*
2699 2706 * In the case of an STT_SECTION symbol, the addend of the
2700 2707 * relocation gives the offset into the string section. For
2701 2708 * other symbol types, the symbol value is the offset.
2702 2709 */
2703 2710
2704 2711 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2705 2712 str_off = sdp->sd_sym->st_value;
2706 2713 } else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2707 2714 /*
2708 2715 * For SHT_RELA, the addend value is found in the
2709 2716 * rel_raddend field of the relocation.
2710 2717 */
2711 2718 str_off = rsp->rel_raddend;
2712 2719 } else { /* REL and STT_SECTION */
2713 2720 /*
2714 2721 * For SHT_REL, the "addend" is not part of the relocation
2715 2722 * record. Instead, it is found at the relocation target
2716 2723 * address.
2717 2724 */
2718 2725 uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2719 2726 (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2720 2727
2721 2728 if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2722 2729 return (0);
2723 2730 }
2724 2731
2725 2732 return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2726 2733 }
2727 2734
2728 2735 /*
2729 2736 * First pass over the relocation records for string table merging.
2730 2737 * Build lists of relocations and symbols that will need modification,
2731 2738 * and insert the strings they reference into the mstrtab string table.
2732 2739 *
2733 2740 * entry:
2734 2741 * ofl, osp - As passed to ld_make_strmerge().
2735 2742 * mstrtab - String table to receive input strings. This table
2736 2743 * must be in its first (initialization) pass and not
2737 2744 * yet cooked (st_getstrtab_sz() not yet called).
2738 2745 * rel_alpp - APlist to receive pointer to any relocation
2739 2746 * descriptors with STT_SECTION symbols that reference
2740 2747 * one of the input sections being merged.
2741 2748 * sym_alpp - APlist to receive pointer to any symbols that reference
2742 2749 * one of the input sections being merged.
2743 2750 * rcp - Pointer to cache of relocation descriptors to examine.
2744 2751 * Either &ofl->ofl_actrels (active relocations)
2745 2752 * or &ofl->ofl_outrels (output relocations).
2746 2753 *
2747 2754 * exit:
2748 2755 * On success, rel_alpp and sym_alpp are updated, and
2749 2756 * any strings in the mergeable input sections referenced by
2750 2757 * a relocation has been entered into mstrtab. True (1) is returned.
2751 2758 *
2752 2759 * On failure, False (0) is returned.
2753 2760 */
2754 2761 static int
2755 2762 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab,
2756 2763 APlist **rel_alpp, APlist **sym_alpp, Rel_cache *rcp)
2757 2764 {
2758 2765 Aliste idx;
2759 2766 Rel_cachebuf *rcbp;
2760 2767 Sym_desc *sdp;
2761 2768 Sym_desc *last_sdp = NULL;
2762 2769 Rel_desc *rsp;
2763 2770 const char *name;
2764 2771
2765 2772 REL_CACHE_TRAVERSE(rcp, idx, rcbp, rsp) {
2766 2773 sdp = rsp->rel_sym;
2767 2774 if ((sdp->sd_isc == NULL) || ((sdp->sd_isc->is_flags &
2768 2775 (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) != FLG_IS_INSTRMRG) ||
2769 2776 (sdp->sd_isc->is_osdesc != osp))
2770 2777 continue;
2771 2778
2772 2779 /*
2773 2780 * Remember symbol for use in the third pass. There is no
2774 2781 * reason to save a given symbol more than once, so we take
2775 2782 * advantage of the fact that relocations to a given symbol
2776 2783 * tend to cluster in the list. If this is the same symbol
2777 2784 * we saved last time, don't bother.
2778 2785 */
2779 2786 if (last_sdp != sdp) {
2780 2787 if (aplist_append(sym_alpp, sdp, AL_CNT_STRMRGSYM) ==
2781 2788 NULL)
2782 2789 return (0);
2783 2790 last_sdp = sdp;
2784 2791 }
2785 2792
2786 2793 /* Enter the string into our new string table */
2787 2794 name = strmerge_get_reloc_str(ofl, rsp);
2788 2795 if (st_insert(mstrtab, name) == -1)
2789 2796 return (0);
2790 2797
2791 2798 /*
2792 2799 * If this is an STT_SECTION symbol, then the second pass
2793 2800 * will need to modify this relocation, so hang on to it.
2794 2801 */
2795 2802 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
2796 2803 (aplist_append(rel_alpp, rsp, AL_CNT_STRMRGREL) == NULL))
2797 2804 return (0);
2798 2805 }
2799 2806
2800 2807 return (1);
2801 2808 }
2802 2809
2803 2810 /*
2804 2811 * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2805 2812 * replace them with a single merged/compressed input section.
2806 2813 *
2807 2814 * entry:
2808 2815 * ofl - Output file descriptor
2809 2816 * osp - Output section descriptor
2810 2817 * rel_alpp, sym_alpp, - Address of 2 APlists, to be used
2811 2818 * for internal processing. On the initial call to
2812 2819 * ld_make_strmerge, these list pointers must be NULL.
2813 2820 * The caller is encouraged to pass the same lists back for
2814 2821 * successive calls to this function without freeing
2815 2822 * them in between calls. This causes a single pair of
2816 2823 * memory allocations to be reused multiple times.
2817 2824 *
2818 2825 * exit:
2819 2826 * If section merging is possible, it is done. If no errors are
2820 2827 * encountered, True (1) is returned. On error, S_ERROR.
2821 2828 *
2822 2829 * The contents of rel_alpp and sym_alpp on exit are
2823 2830 * undefined. The caller can free them, or pass them back to a subsequent
2824 2831 * call to this routine, but should not examine their contents.
2825 2832 */
2826 2833 static uintptr_t
2827 2834 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_alpp,
2828 2835 APlist **sym_alpp)
2829 2836 {
2830 2837 Str_tbl *mstrtab; /* string table for string merge secs */
2831 2838 Is_desc *mstrsec; /* Generated string merge section */
2832 2839 Is_desc *isp;
2833 2840 Shdr *mstr_shdr;
2834 2841 Elf_Data *mstr_data;
2835 2842 Sym_desc *sdp;
2836 2843 Rel_desc *rsp;
2837 2844 Aliste idx;
2838 2845 size_t data_size;
2839 2846 int st_setstring_status;
2840 2847 size_t stoff;
2841 2848
↓ open down ↓ |
2604 lines elided |
↑ open up ↑ |
2842 2849 /* If string table compression is disabled, there's nothing to do */
2843 2850 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0)
2844 2851 return (1);
2845 2852
2846 2853 /*
2847 2854 * Pass over the mergeable input sections, and if they haven't
2848 2855 * all been discarded, create a string table.
2849 2856 */
2850 2857 mstrtab = NULL;
2851 2858 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2852 - if (isp->is_flags & FLG_IS_DISCARD)
2859 + if (isdesc_discarded(isp))
2860 + continue;
2861 +
2862 + /*
2863 + * Input sections of 0 size are dubiously valid since they do
2864 + * not even contain the NUL string. Ignore them.
2865 + */
2866 + if (isp->is_shdr->sh_size == 0)
2853 2867 continue;
2854 2868
2855 2869 /*
2856 2870 * We have at least one non-discarded section.
2857 2871 * Create a string table descriptor.
2858 2872 */
2859 2873 if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2860 2874 return (S_ERROR);
2861 2875 break;
2862 2876 }
2863 2877
2864 2878 /* If no string table was created, we have no mergeable sections */
2865 2879 if (mstrtab == NULL)
2866 2880 return (1);
2867 2881
2868 2882 /*
2869 2883 * This routine has to make 3 passes:
2870 2884 *
2871 2885 * 1) Examine all relocations, insert strings from relocations
2872 2886 * to the mergeable input sections into the string table.
2873 2887 * 2) Modify the relocation values to be correct for the
2874 2888 * new merged section.
2875 2889 * 3) Modify the symbols used by the relocations to reference
2876 2890 * the new section.
2877 2891 *
2878 2892 * These passes cannot be combined:
2879 2893 * - The string table code works in two passes, and all
2880 2894 * strings have to be loaded in pass one before the
2881 2895 * offset of any strings can be determined.
2882 2896 * - Multiple relocations reference a single symbol, so the
2883 2897 * symbol cannot be modified until all relocations are
2884 2898 * fixed.
2885 2899 *
2886 2900 * The number of relocations related to section merging is usually
2887 2901 * a mere fraction of the overall active and output relocation lists,
2888 2902 * and the number of symbols is usually a fraction of the number
2889 2903 * of related relocations. We therefore build APlists for the
2890 2904 * relocations and symbols in the first pass, and then use those
2891 2905 * lists to accelerate the operation of pass 2 and 3.
2892 2906 *
2893 2907 * Reinitialize the lists to a completely empty state.
2894 2908 */
2895 2909 aplist_reset(*rel_alpp);
2896 2910 aplist_reset(*sym_alpp);
2897 2911
2898 2912 /*
2899 2913 * Pass 1:
2900 2914 *
2901 2915 * Every relocation related to this output section (and the input
2902 2916 * sections that make it up) is found in either the active, or the
2903 2917 * output relocation list, depending on whether the relocation is to
2904 2918 * be processed by this invocation of the linker, or inserted into the
2905 2919 * output object.
2906 2920 *
2907 2921 * Build lists of relocations and symbols that will need modification,
2908 2922 * and insert the strings they reference into the mstrtab string table.
2909 2923 */
2910 2924 if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2911 2925 &ofl->ofl_actrels) == 0)
2912 2926 goto return_s_error;
2913 2927 if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2914 2928 &ofl->ofl_outrels) == 0)
2915 2929 goto return_s_error;
2916 2930
2917 2931 /*
2918 2932 * Get the size of the new input section. Requesting the
2919 2933 * string table size "cooks" the table, and finalizes its contents.
2920 2934 */
2921 2935 data_size = st_getstrtab_sz(mstrtab);
2922 2936
2923 2937 /* Create a new input section to hold the merged strings */
2924 2938 if (new_section_from_template(ofl, isp, data_size,
2925 2939 &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR)
2926 2940 goto return_s_error;
2927 2941 mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2928 2942
2929 2943 /*
2930 2944 * Allocate a data buffer for the new input section.
2931 2945 * Then, associate the buffer with the string table descriptor.
2932 2946 */
2933 2947 if ((mstr_data->d_buf = libld_malloc(data_size)) == NULL)
2934 2948 goto return_s_error;
2935 2949 if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1)
2936 2950 goto return_s_error;
2937 2951
2938 2952 /* Add the new section to the output image */
2939 2953 if (ld_place_section(ofl, mstrsec, NULL, osp->os_identndx, NULL) ==
2940 2954 (Os_desc *)S_ERROR)
2941 2955 goto return_s_error;
2942 2956
2943 2957 /*
2944 2958 * Pass 2:
2945 2959 *
2946 2960 * Revisit the relocation descriptors with STT_SECTION symbols
2947 2961 * that were saved by the first pass. Update each relocation
2948 2962 * record so that the offset it contains is for the new section
2949 2963 * instead of the original.
2950 2964 */
2951 2965 for (APLIST_TRAVERSE(*rel_alpp, idx, rsp)) {
2952 2966 const char *name;
2953 2967
2954 2968 /* Put the string into the merged string table */
2955 2969 name = strmerge_get_reloc_str(ofl, rsp);
2956 2970 st_setstring_status = st_setstring(mstrtab, name, &stoff);
2957 2971 if (st_setstring_status == -1) {
2958 2972 /*
2959 2973 * A failure to insert at this point means that
2960 2974 * something is corrupt. This isn't a resource issue.
2961 2975 */
2962 2976 assert(st_setstring_status != -1);
2963 2977 goto return_s_error;
2964 2978 }
2965 2979
2966 2980 /*
2967 2981 * Alter the relocation to access the string at the
2968 2982 * new offset in our new string table.
2969 2983 *
2970 2984 * For SHT_RELA platforms, it suffices to simply
2971 2985 * update the rel_raddend field of the relocation.
2972 2986 *
2973 2987 * For SHT_REL platforms, the new "addend" value
2974 2988 * needs to be written at the address being relocated.
2975 2989 * However, we can't alter the input sections which
2976 2990 * are mapped readonly, and the output image has not
2977 2991 * been created yet. So, we defer this operation,
2978 2992 * using the rel_raddend field of the relocation
2979 2993 * which is normally 0 on a REL platform, to pass the
2980 2994 * new "addend" value to ld_perform_outreloc() or
2981 2995 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2982 2996 * tells them that this is the case.
2983 2997 */
2984 2998 if ((rsp->rel_flags & FLG_REL_RELA) == 0) /* REL */
2985 2999 rsp->rel_flags |= FLG_REL_NADDEND;
2986 3000 rsp->rel_raddend = (Sxword)stoff;
2987 3001
2988 3002 /*
2989 3003 * Generate a symbol name string for STT_SECTION symbols
2990 3004 * that might reference our merged section. This shows up
2991 3005 * in debug output and helps show how the relocation has
2992 3006 * changed from its original input section to our merged one.
2993 3007 */
2994 3008 if (ld_stt_section_sym_name(mstrsec) == NULL)
2995 3009 goto return_s_error;
2996 3010 }
2997 3011
2998 3012 /*
2999 3013 * Pass 3:
3000 3014 *
3001 3015 * Modify the symbols referenced by the relocation descriptors
3002 3016 * so that they reference the new input section containing the
3003 3017 * merged strings instead of the original input sections.
3004 3018 */
3005 3019 for (APLIST_TRAVERSE(*sym_alpp, idx, sdp)) {
3006 3020 /*
3007 3021 * If we've already processed this symbol, don't do it
3008 3022 * twice. strmerge_pass1() uses a heuristic (relocations to
3009 3023 * the same symbol clump together) to avoid inserting a
3010 3024 * given symbol more than once, but repeat symbols in
3011 3025 * the list can occur.
3012 3026 */
3013 3027 if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
3014 3028 continue;
3015 3029
3016 3030 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
3017 3031 /*
3018 3032 * This is not an STT_SECTION symbol, so its
3019 3033 * value is the offset of the string within the
3020 3034 * input section. Update the address to reflect
3021 3035 * the address in our new merged section.
3022 3036 */
3023 3037 const char *name = sdp->sd_sym->st_value +
3024 3038 (char *)sdp->sd_isc->is_indata->d_buf;
3025 3039
3026 3040 st_setstring_status =
3027 3041 st_setstring(mstrtab, name, &stoff);
3028 3042 if (st_setstring_status == -1) {
3029 3043 /*
3030 3044 * A failure to insert at this point means
3031 3045 * something is corrupt. This isn't a
3032 3046 * resource issue.
3033 3047 */
3034 3048 assert(st_setstring_status != -1);
3035 3049 goto return_s_error;
3036 3050 }
3037 3051
3038 3052 if (ld_sym_copy(sdp) == S_ERROR)
3039 3053 goto return_s_error;
3040 3054 sdp->sd_sym->st_value = (Word)stoff;
3041 3055 }
3042 3056
3043 3057 /* Redirect the symbol to our new merged section */
3044 3058 sdp->sd_isc = mstrsec;
3045 3059 }
3046 3060
3047 3061 /*
3048 3062 * There are no references left to the original input string sections.
3049 3063 * Mark them as discarded so they don't go into the output image.
3050 3064 * At the same time, add up the sizes of the replaced sections.
3051 3065 */
3052 3066 data_size = 0;
3053 3067 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
3054 3068 if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
3055 3069 continue;
3056 3070
3057 3071 data_size += isp->is_indata->d_size;
3058 3072
3059 3073 isp->is_flags |= FLG_IS_DISCARD;
3060 3074 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
3061 3075 }
3062 3076
3063 3077 /* Report how much space we saved in the output section */
3064 3078 DBG_CALL(Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
3065 3079 mstr_data->d_size));
3066 3080
3067 3081 st_destroy(mstrtab);
3068 3082 return (1);
3069 3083
3070 3084 return_s_error:
3071 3085 st_destroy(mstrtab);
3072 3086 return (S_ERROR);
3073 3087 }
3074 3088
3075 3089 /*
3076 3090 * Update a data buffers size. A number of sections have to be created, and
3077 3091 * the sections header contributes to the size of the eventual section. Thus,
3078 3092 * a section may be created, and once all associated sections have been created,
3079 3093 * we return to establish the required section size.
3080 3094 */
3081 3095 inline static void
3082 3096 update_data_size(Os_desc *osp, ulong_t cnt)
3083 3097 {
3084 3098 Is_desc *isec = ld_os_first_isdesc(osp);
3085 3099 Elf_Data *data = isec->is_indata;
3086 3100 Shdr *shdr = osp->os_shdr;
3087 3101 size_t size = cnt * shdr->sh_entsize;
3088 3102
3089 3103 shdr->sh_size = (Xword)size;
3090 3104 data->d_size = size;
3091 3105 }
3092 3106
3093 3107 /*
3094 3108 * The following sections are built after all input file processing and symbol
3095 3109 * validation has been carried out. The order is important (because the
3096 3110 * addition of a section adds a new symbol there is a chicken and egg problem
3097 3111 * of maintaining the appropriate counts). By maintaining a known order the
3098 3112 * individual routines can compensate for later, known, additions.
3099 3113 */
3100 3114 uintptr_t
3101 3115 ld_make_sections(Ofl_desc *ofl)
3102 3116 {
3103 3117 ofl_flag_t flags = ofl->ofl_flags;
3104 3118 Sg_desc *sgp;
3105 3119
3106 3120 /*
3107 3121 * Generate any special sections.
3108 3122 */
3109 3123 if (flags & FLG_OF_ADDVERS)
3110 3124 if (make_comment(ofl) == S_ERROR)
3111 3125 return (S_ERROR);
3112 3126
3113 3127 if (make_interp(ofl) == S_ERROR)
3114 3128 return (S_ERROR);
3115 3129
3116 3130 /*
3117 3131 * Create a capabilities section if required.
3118 3132 */
3119 3133 if (make_cap(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP),
3120 3134 ld_targ.t_id.id_cap) == S_ERROR)
3121 3135 return (S_ERROR);
3122 3136
3123 3137 /*
3124 3138 * Create any init/fini array sections.
3125 3139 */
3126 3140 if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
3127 3141 ofl->ofl_initarray) == S_ERROR)
3128 3142 return (S_ERROR);
3129 3143
3130 3144 if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
3131 3145 ofl->ofl_finiarray) == S_ERROR)
3132 3146 return (S_ERROR);
3133 3147
3134 3148 if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
3135 3149 ofl->ofl_preiarray) == S_ERROR)
3136 3150 return (S_ERROR);
3137 3151
3138 3152 /*
3139 3153 * Make the .plt section. This occurs after any other relocation
3140 3154 * sections are generated (see reloc_init()) to ensure that the
3141 3155 * associated relocation section is after all the other relocation
3142 3156 * sections.
3143 3157 */
3144 3158 if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
3145 3159 if (make_plt(ofl) == S_ERROR)
3146 3160 return (S_ERROR);
3147 3161
3148 3162 /*
3149 3163 * Determine whether any sections or files are not referenced. Under
3150 3164 * -Dunused a diagnostic for any unused components is generated, under
3151 3165 * -zignore the component is removed from the final output.
3152 3166 */
3153 3167 if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
3154 3168 if (ignore_section_processing(ofl) == S_ERROR)
3155 3169 return (S_ERROR);
3156 3170 }
3157 3171
3158 3172 /*
3159 3173 * If we have detected a situation in which previously placed
3160 3174 * output sections may have been discarded, perform the necessary
3161 3175 * readjustment.
3162 3176 */
3163 3177 if (ofl->ofl_flags & FLG_OF_ADJOSCNT)
3164 3178 adjust_os_count(ofl);
3165 3179
3166 3180 /*
3167 3181 * Do any of the output sections contain input sections that
3168 3182 * are candidates for string table merging? For each such case,
3169 3183 * we create a replacement section, insert it, and discard the
3170 3184 * originals.
3171 3185 *
3172 3186 * rel_alpp and sym_alpp are used by ld_make_strmerge()
3173 3187 * for its internal processing. We are responsible for the
3174 3188 * initialization and cleanup, and ld_make_strmerge() handles the rest.
3175 3189 * This allows us to reuse a single pair of memory buffers, allocated
3176 3190 * for this processing, for all the output sections.
3177 3191 */
3178 3192 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
3179 3193 int error_seen = 0;
3180 3194 APlist *rel_alpp = NULL;
3181 3195 APlist *sym_alpp = NULL;
3182 3196 Aliste idx1;
3183 3197
3184 3198 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3185 3199 Os_desc *osp;
3186 3200 Aliste idx2;
3187 3201
3188 3202 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp))
3189 3203 if ((osp->os_mstrisdescs != NULL) &&
3190 3204 (ld_make_strmerge(ofl, osp,
3191 3205 &rel_alpp, &sym_alpp) ==
3192 3206 S_ERROR)) {
3193 3207 error_seen = 1;
3194 3208 break;
3195 3209 }
3196 3210 }
3197 3211 if (rel_alpp != NULL)
3198 3212 libld_free(rel_alpp);
3199 3213 if (sym_alpp != NULL)
3200 3214 libld_free(sym_alpp);
3201 3215 if (error_seen != 0)
3202 3216 return (S_ERROR);
3203 3217 }
3204 3218
3205 3219 /*
3206 3220 * Add any necessary versioning information.
3207 3221 */
3208 3222 if (!(flags & FLG_OF_NOVERSEC)) {
3209 3223 if ((flags & FLG_OF_VERNEED) &&
3210 3224 (make_verneed(ofl) == S_ERROR))
3211 3225 return (S_ERROR);
3212 3226 if ((flags & FLG_OF_VERDEF) &&
3213 3227 (make_verdef(ofl) == S_ERROR))
3214 3228 return (S_ERROR);
3215 3229 if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
3216 3230 ((ofl->ofl_osversym = make_sym_sec(ofl,
3217 3231 MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
3218 3232 ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
3219 3233 return (S_ERROR);
3220 3234 }
3221 3235
3222 3236 /*
3223 3237 * Create a syminfo section if necessary.
3224 3238 */
3225 3239 if (flags & FLG_OF_SYMINFO) {
3226 3240 if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
3227 3241 MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
3228 3242 ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
3229 3243 return (S_ERROR);
3230 3244 }
3231 3245
3232 3246 if (flags & FLG_OF_COMREL) {
3233 3247 /*
3234 3248 * If -zcombreloc is enabled then all relocations (except for
3235 3249 * the PLT's) are coalesced into a single relocation section.
3236 3250 */
3237 3251 if (ofl->ofl_reloccnt) {
3238 3252 if (make_reloc(ofl, NULL) == S_ERROR)
3239 3253 return (S_ERROR);
3240 3254 }
3241 3255 } else {
3242 3256 Aliste idx1;
3243 3257
3244 3258 /*
3245 3259 * Create the required output relocation sections. Note, new
3246 3260 * sections may be added to the section list that is being
3247 3261 * traversed. These insertions can move the elements of the
3248 3262 * Alist such that a section descriptor is re-read. Recursion
3249 3263 * is prevented by maintaining a previous section pointer and
3250 3264 * insuring that this pointer isn't re-examined.
3251 3265 */
3252 3266 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3253 3267 Os_desc *osp, *posp = 0;
3254 3268 Aliste idx2;
3255 3269
3256 3270 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3257 3271 if ((osp != posp) && osp->os_szoutrels &&
3258 3272 (osp != ofl->ofl_osplt)) {
3259 3273 if (make_reloc(ofl, osp) == S_ERROR)
3260 3274 return (S_ERROR);
3261 3275 }
3262 3276 posp = osp;
3263 3277 }
3264 3278 }
3265 3279
3266 3280 /*
3267 3281 * If we're not building a combined relocation section, then
3268 3282 * build a .rel[a] section as required.
3269 3283 */
3270 3284 if (ofl->ofl_relocrelsz) {
3271 3285 if (make_reloc(ofl, NULL) == S_ERROR)
3272 3286 return (S_ERROR);
3273 3287 }
3274 3288 }
3275 3289
3276 3290 /*
3277 3291 * The PLT relocations are always in their own section, and we try to
3278 3292 * keep them at the end of the PLT table. We do this to keep the hot
3279 3293 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
3280 3294 */
3281 3295 if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
3282 3296 if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
3283 3297 return (S_ERROR);
3284 3298 }
3285 3299
3286 3300 /*
3287 3301 * Finally build the symbol and section header sections.
3288 3302 */
3289 3303 if (flags & FLG_OF_DYNAMIC) {
3290 3304 if (make_dynamic(ofl) == S_ERROR)
3291 3305 return (S_ERROR);
3292 3306
3293 3307 /*
3294 3308 * A number of sections aren't necessary within a relocatable
3295 3309 * object, even if -dy has been used.
3296 3310 */
3297 3311 if (!(flags & FLG_OF_RELOBJ)) {
3298 3312 if (make_hash(ofl) == S_ERROR)
3299 3313 return (S_ERROR);
3300 3314 if (make_dynstr(ofl) == S_ERROR)
3301 3315 return (S_ERROR);
3302 3316 if (make_dynsym(ofl) == S_ERROR)
3303 3317 return (S_ERROR);
3304 3318 if (ld_unwind_make_hdr(ofl) == S_ERROR)
3305 3319 return (S_ERROR);
3306 3320 if (make_dynsort(ofl) == S_ERROR)
3307 3321 return (S_ERROR);
3308 3322 }
3309 3323 }
3310 3324
3311 3325 if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
3312 3326 ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
3313 3327 /*
3314 3328 * Do we need to make a SHT_SYMTAB_SHNDX section
3315 3329 * for the dynsym. If so - do it now.
3316 3330 */
3317 3331 if (ofl->ofl_osdynsym &&
3318 3332 ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
3319 3333 if (make_dynsym_shndx(ofl) == S_ERROR)
3320 3334 return (S_ERROR);
3321 3335 }
3322 3336
3323 3337 if (make_strtab(ofl) == S_ERROR)
3324 3338 return (S_ERROR);
3325 3339 if (make_symtab(ofl) == S_ERROR)
3326 3340 return (S_ERROR);
3327 3341 } else {
3328 3342 /*
3329 3343 * Do we need to make a SHT_SYMTAB_SHNDX section
3330 3344 * for the dynsym. If so - do it now.
3331 3345 */
3332 3346 if (ofl->ofl_osdynsym &&
3333 3347 ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
3334 3348 if (make_dynsym_shndx(ofl) == S_ERROR)
3335 3349 return (S_ERROR);
3336 3350 }
3337 3351 }
3338 3352
3339 3353 if (make_shstrtab(ofl) == S_ERROR)
3340 3354 return (S_ERROR);
3341 3355
3342 3356 /*
3343 3357 * Now that we've created all output sections, adjust the size of the
3344 3358 * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on
3345 3359 * the associated symbol table sizes.
3346 3360 */
3347 3361 if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
3348 3362 ulong_t cnt;
3349 3363 Is_desc *isp;
3350 3364 Os_desc *osp;
3351 3365
3352 3366 if (OFL_IS_STATIC_OBJ(ofl))
3353 3367 osp = ofl->ofl_ossymtab;
3354 3368 else
3355 3369 osp = ofl->ofl_osdynsym;
3356 3370
3357 3371 isp = ld_os_first_isdesc(osp);
3358 3372 cnt = (isp->is_shdr->sh_size / isp->is_shdr->sh_entsize);
3359 3373
3360 3374 if (ofl->ofl_osversym)
3361 3375 update_data_size(ofl->ofl_osversym, cnt);
3362 3376
3363 3377 if (ofl->ofl_ossyminfo)
3364 3378 update_data_size(ofl->ofl_ossyminfo, cnt);
3365 3379 }
3366 3380
3367 3381 /*
3368 3382 * Now that we've created all output sections, adjust the size of the
3369 3383 * SHT_SUNW_capinfo, which is dependent on the associated symbol table
3370 3384 * size.
3371 3385 */
3372 3386 if (ofl->ofl_oscapinfo) {
3373 3387 ulong_t cnt;
3374 3388
3375 3389 /*
3376 3390 * Symbol capabilities symbols are placed directly after the
3377 3391 * STT_FILE symbol, section symbols, and any register symbols.
3378 3392 * Effectively these are the first of any series of demoted
3379 3393 * (scoped) symbols.
3380 3394 */
3381 3395 if (OFL_IS_STATIC_OBJ(ofl))
3382 3396 cnt = SYMTAB_ALL_CNT(ofl);
3383 3397 else
3384 3398 cnt = DYNSYM_ALL_CNT(ofl);
3385 3399
3386 3400 update_data_size(ofl->ofl_oscapinfo, cnt);
3387 3401 }
3388 3402 return (1);
3389 3403 }
3390 3404
3391 3405 /*
3392 3406 * Build an additional data section - used to back OBJT symbol definitions
3393 3407 * added with a mapfile.
3394 3408 */
3395 3409 Is_desc *
3396 3410 ld_make_data(Ofl_desc *ofl, size_t size)
3397 3411 {
3398 3412 Shdr *shdr;
3399 3413 Elf_Data *data;
3400 3414 Is_desc *isec;
3401 3415
3402 3416 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
3403 3417 &isec, &shdr, &data) == S_ERROR)
3404 3418 return ((Is_desc *)S_ERROR);
3405 3419
3406 3420 data->d_size = size;
3407 3421 shdr->sh_size = (Xword)size;
3408 3422 shdr->sh_flags |= SHF_WRITE;
3409 3423
3410 3424 if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL)
3411 3425 return ((Is_desc *)S_ERROR);
3412 3426
3413 3427 return (isec);
3414 3428 }
3415 3429
3416 3430 /*
3417 3431 * Build an additional text section - used to back FUNC symbol definitions
3418 3432 * added with a mapfile.
3419 3433 */
3420 3434 Is_desc *
3421 3435 ld_make_text(Ofl_desc *ofl, size_t size)
3422 3436 {
3423 3437 Shdr *shdr;
3424 3438 Elf_Data *data;
3425 3439 Is_desc *isec;
3426 3440
3427 3441 /*
3428 3442 * Insure the size is sufficient to contain the minimum return
3429 3443 * instruction.
3430 3444 */
3431 3445 if (size < ld_targ.t_nf.nf_size)
3432 3446 size = ld_targ.t_nf.nf_size;
3433 3447
3434 3448 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
3435 3449 &isec, &shdr, &data) == S_ERROR)
3436 3450 return ((Is_desc *)S_ERROR);
3437 3451
3438 3452 data->d_size = size;
3439 3453 shdr->sh_size = (Xword)size;
3440 3454 shdr->sh_flags |= SHF_EXECINSTR;
3441 3455
3442 3456 /*
3443 3457 * Fill the buffer with the appropriate return instruction.
3444 3458 * Note that there is no need to swap bytes on a non-native,
3445 3459 * link, as the data being copied is given in bytes.
3446 3460 */
3447 3461 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
3448 3462 return ((Is_desc *)S_ERROR);
3449 3463 (void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
3450 3464 ld_targ.t_nf.nf_size);
3451 3465
3452 3466 /*
3453 3467 * If size was larger than required, and the target supplies
3454 3468 * a fill function, use it to fill the balance. If there is no
3455 3469 * fill function, we accept the 0-fill supplied by libld_calloc().
3456 3470 */
3457 3471 if ((ld_targ.t_ff.ff_execfill != NULL) && (size > ld_targ.t_nf.nf_size))
3458 3472 ld_targ.t_ff.ff_execfill(data->d_buf, ld_targ.t_nf.nf_size,
3459 3473 size - ld_targ.t_nf.nf_size);
3460 3474
3461 3475 if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL)
3462 3476 return ((Is_desc *)S_ERROR);
3463 3477
3464 3478 return (isec);
3465 3479 }
3466 3480
3467 3481 void
3468 3482 ld_comdat_validate(Ofl_desc *ofl, Ifl_desc *ifl)
3469 3483 {
3470 3484 int i;
3471 3485
3472 3486 for (i = 0; i < ifl->ifl_shnum; i++) {
3473 3487 Is_desc *isp = ifl->ifl_isdesc[i];
3474 3488 int types = 0;
3475 3489 char buf[1024] = "";
3476 3490 Group_desc *gr = NULL;
3477 3491
3478 3492 if ((isp == NULL) || (isp->is_flags & FLG_IS_COMDAT) == 0)
3479 3493 continue;
3480 3494
3481 3495 if (isp->is_shdr->sh_type == SHT_SUNW_COMDAT) {
3482 3496 types++;
3483 3497 (void) strlcpy(buf, MSG_ORIG(MSG_STR_SUNW_COMDAT),
3484 3498 sizeof (buf));
3485 3499 }
3486 3500
3487 3501 if (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
3488 3502 MSG_SCN_GNU_LINKONCE_SIZE) == 0) {
3489 3503 types++;
3490 3504 if (types > 1)
3491 3505 (void) strlcat(buf, ", ", sizeof (buf));
3492 3506 (void) strlcat(buf, MSG_ORIG(MSG_SCN_GNU_LINKONCE),
3493 3507 sizeof (buf));
3494 3508 }
3495 3509
3496 3510 if ((isp->is_shdr->sh_flags & SHF_GROUP) &&
3497 3511 ((gr = ld_get_group(ofl, isp)) != NULL) &&
3498 3512 (gr->gd_data[0] & GRP_COMDAT)) {
3499 3513 types++;
3500 3514 if (types > 1)
3501 3515 (void) strlcat(buf, ", ", sizeof (buf));
3502 3516 (void) strlcat(buf, MSG_ORIG(MSG_STR_GROUP),
3503 3517 sizeof (buf));
3504 3518 }
3505 3519
3506 3520 if (types > 1)
3507 3521 ld_eprintf(ofl, ERR_FATAL,
3508 3522 MSG_INTL(MSG_SCN_MULTICOMDAT), ifl->ifl_name,
3509 3523 EC_WORD(isp->is_scnndx), isp->is_name, buf);
3510 3524 }
3511 3525 }
↓ open down ↓ |
649 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX