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