This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
sections.c
2942 lines (2546 loc) · 77.3 KB
/
sections.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1988 AT&T
* All Rights Reserved
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Module sections. Initialize special sections
*/
#define ELF_TARGET_AMD64
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <link.h>
#include <debug.h>
#include "msg.h"
#include "_libld.h"
inline static void
remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
{
Sym *sym = sdp->sd_sym;
uchar_t type = ELF_ST_TYPE(sym->st_info);
/* LINTED - only used for assert() */
int err;
if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) {
ofl->ofl_locscnt--;
err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
assert(err != -1);
if (allow_ldynsym && ldynsym_symtype[type]) {
ofl->ofl_dynlocscnt--;
err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
assert(err != -1);
/* Remove from sort section? */
DYNSORT_COUNT(sdp, sym, type, --);
}
}
sdp->sd_flags |= FLG_SY_ISDISC;
}
inline static void
remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
{
Sym *sym = sdp->sd_sym;
uchar_t type = ELF_ST_TYPE(sym->st_info);
/* LINTED - only used for assert() */
int err;
ofl->ofl_scopecnt--;
ofl->ofl_elimcnt++;
err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
assert(err != -1);
if (allow_ldynsym && ldynsym_symtype[type]) {
ofl->ofl_dynscopecnt--;
err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
assert(err != -1);
/* Remove from sort section? */
DYNSORT_COUNT(sdp, sym, type, --);
}
sdp->sd_flags |= FLG_SY_ELIM;
}
inline static void
ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym)
{
Os_desc *osp;
Is_desc *isp = sdp->sd_isc;
uchar_t bind = ELF_ST_BIND(sdp->sd_sym->st_info);
if (bind == STB_LOCAL) {
uchar_t type = ELF_ST_TYPE(sdp->sd_sym->st_info);
/*
* Skip section symbols, these were never collected in the
* first place.
*/
if (type == STT_SECTION)
return;
/*
* Determine if the whole file is being removed. Remove any
* file symbol, and any symbol that is not associated with a
* section, provided the symbol has not been identified as
* (update) required.
*/
if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) &&
((type == STT_FILE) || ((isp == NULL) &&
((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) {
DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
if (ifl->ifl_flags & FLG_IF_IGNORE)
remove_local(ofl, sdp, allow_ldynsym);
return;
}
} else {
/*
* Global symbols can only be eliminated when the interfaces of
* an object have been defined via versioning/scoping.
*/
if ((sdp->sd_flags & FLG_SY_HIDDEN) == 0)
return;
/*
* Remove any unreferenced symbols that are not associated with
* a section.
*/
if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) {
DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
if (ifl->ifl_flags & FLG_IF_IGNORE)
remove_scoped(ofl, sdp, allow_ldynsym);
return;
}
}
/*
* Do not discard any symbols that are associated with non-allocable
* segments.
*/
if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
((osp = isp->is_osdesc) != 0) &&
(osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) {
DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
if (ifl->ifl_flags & FLG_IF_IGNORE) {
if (bind == STB_LOCAL)
remove_local(ofl, sdp, allow_ldynsym);
else
remove_scoped(ofl, sdp, allow_ldynsym);
}
}
}
/*
* If -zignore has been in effect, scan all input files to determine if the
* file, or sections from the file, have been referenced. If not, the file or
* some of the files sections can be discarded. If sections are to be
* discarded, rescan the output relocations and the symbol table and remove
* the relocations and symbol entries that are no longer required.
*
* Note: It's possible that a section which is being discarded has contributed
* to the GOT table or the PLT table. However, we can't at this point
* eliminate the corresponding entries. This is because there could well
* be other sections referencing those same entries, but we don't have
* the infrastructure to determine this. So, keep the PLT and GOT
* entries in the table in case someone wants them.
* Note: The section to be affected needs to be allocatable.
* So even if -zignore is in effect, if the section is not allocatable,
* we do not eliminate it.
*/
static uintptr_t
ignore_section_processing(Ofl_desc *ofl)
{
Sg_desc *sgp;
Is_desc *isp;
Os_desc *osp;
Ifl_desc *ifl;
Rel_cache *rcp;
int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
Aliste idx1;
for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
uint_t num, discard;
/*
* Diagnose (-D unused) a completely unreferenced file.
*/
if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
DBG_CALL(Dbg_unused_file(ofl->ofl_lml,
ifl->ifl_name, 0, 0));
if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) ||
((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
continue;
/*
* Before scanning the whole symbol table to determine if
* symbols should be discard - quickly (relatively) scan the
* sections to determine if any are to be discarded.
*/
discard = 0;
if (ifl->ifl_flags & FLG_IF_FILEREF) {
for (num = 1; num < ifl->ifl_shnum; num++) {
if (((isp = ifl->ifl_isdesc[num]) != NULL) &&
((isp->is_flags & FLG_IS_SECTREF) == 0) &&
((osp = isp->is_osdesc) != NULL) &&
((sgp = osp->os_sgdesc) != NULL) &&
(sgp->sg_phdr.p_type == PT_LOAD)) {
discard++;
break;
}
}
}
/*
* No sections are to be 'ignored'
*/
if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF))
continue;
/*
* We know that we have discarded sections. Scan the symbol
* table for this file to determine if symbols need to be
* discarded that are associated with the 'ignored' sections.
*/
for (num = 1; num < ifl->ifl_symscnt; num++) {
Sym_desc *sdp;
/*
* If the symbol definition has been resolved to another
* file, or the symbol has already been discarded or
* eliminated, skip it.
*/
sdp = ifl->ifl_oldndx[num];
if ((sdp->sd_file != ifl) ||
(sdp->sd_flags &
(FLG_SY_ISDISC | FLG_SY_INVALID | FLG_SY_ELIM)))
continue;
/*
* Complete the investigation of the symbol.
*/
ignore_sym(ofl, ifl, sdp, allow_ldynsym);
}
}
/*
* If we were only here to solicit debugging diagnostics, we're done.
*/
if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0)
return (1);
/*
* Scan all output relocations searching for those against discarded or
* ignored sections. If one is found, decrement the total outrel count.
*/
for (APLIST_TRAVERSE(ofl->ofl_outrels, idx1, rcp)) {
Rel_desc *rsp;
/* LINTED */
for (rsp = (Rel_desc *)(rcp + 1); rsp < rcp->rc_free; rsp++) {
Is_desc *isc = rsp->rel_isdesc;
uint_t flags, entsize;
Shdr *shdr;
if ((isc == NULL) ||
((isc->is_flags & (FLG_IS_SECTREF))) ||
((ifl = isc->is_file) == NULL) ||
((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
((shdr = isc->is_shdr) == NULL) ||
((shdr->sh_flags & SHF_ALLOC) == 0))
continue;
flags = rsp->rel_flags;
if (flags & (FLG_REL_GOT | FLG_REL_BSS |
FLG_REL_NOINFO | FLG_REL_PLT))
continue;
osp = rsp->rel_osdesc;
if (rsp->rel_flags & FLG_REL_RELA)
entsize = sizeof (Rela);
else
entsize = sizeof (Rel);
assert(osp->os_szoutrels > 0);
osp->os_szoutrels -= entsize;
if (!(flags & FLG_REL_PLT))
ofl->ofl_reloccntsub++;
if (rsp->rel_rtype == ld_targ.t_m.m_r_relative)
ofl->ofl_relocrelcnt--;
}
}
/*
* The number of output sections may have decreased. We must make a
* pass over the output sections, and if we detect this situation,
* decrement ofl->ofl_shdrcnt and remove the section name from the
* .shstrtab string table (ofl->ofl_shdrsttab).
*
* This code must be kept in sync with the similar code
* found in outfile.c:ld_create_outfile().
*
* For each output section, look at the input sections to find at least
* one input section that has not been eliminated. If none are found,
* the -z ignore processing above has eliminated that output section.
*/
for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
Aliste idx2;
Word ptype = sgp->sg_phdr.p_type;
for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
Aliste idx3;
int keep = 0, os_isdescs_idx;
OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) {
ifl = isp->is_file;
/* Input section is tagged for discard? */
if (isp->is_flags & FLG_IS_DISCARD)
continue;
/*
* If the file is discarded, it will take
* the section with it.
*/
if (ifl &&
(((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
((ptype == PT_LOAD) &&
((isp->is_flags & FLG_IS_SECTREF) == 0) &&
(isp->is_shdr->sh_size > 0))) &&
(ifl->ifl_flags & FLG_IF_IGNORE))
continue;
/*
* We have found a kept input section,
* so the output section will be created.
*/
keep = 1;
break;
}
/*
* If no section of this name was kept, decrement
* the count and remove the name from .shstrtab.
*/
if (keep == 0) {
/* LINTED - only used for assert() */
int err;
ofl->ofl_shdrcnt--;
err = st_delstring(ofl->ofl_shdrsttab,
osp->os_name);
assert(err != -1);
}
}
}
return (1);
}
/*
* Allocate Elf_Data, Shdr, and Is_desc structures for a new
* section.
*
* entry:
* ofl - Output file descriptor
* shtype - SHT_ type code for section.
* shname - String giving the name for the new section.
* entcnt - # of items contained in the data part of the new section.
* This value is multiplied against the known element size
* for the section type to determine the size of the data
* area for the section. It is only meaningful in cases where
* the section type has a non-zero element size. In other cases,
* the caller must set the size fields in the *ret_data and
* *ret_shdr structs manually.
* ret_isec, ret_shdr, ret_data - Address of pointers to
* receive address of newly allocated structs.
*
* exit:
* On error, returns S_ERROR. On success, returns (1), and the
* ret_ pointers have been updated to point at the new structures,
* which have been filled in. To finish the task, the caller must
* update any fields within the supplied descriptors that differ
* from its needs, and then call ld_place_section().
*/
static uintptr_t
new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt,
Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
{
typedef struct sec_info {
Word d_type;
Word align; /* Used in both data and section header */
Word sh_flags;
Word sh_entsize;
} SEC_INFO_T;
const SEC_INFO_T *sec_info;
Shdr *shdr;
Elf_Data *data;
Is_desc *isec;
size_t size;
/*
* For each type of section, we have a distinct set of
* SEC_INFO_T values. This macro defines a static structure
* containing those values and generates code to set the sec_info
* pointer to refer to it. The pointer in sec_info remains valid
* outside of the declaration scope because the info_s struct is static.
*
* We can't determine the value of M_WORD_ALIGN at compile time, so
* a different variant is used for those cases.
*/
#define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
{ \
static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
sh_entsize}; \
sec_info = &info_s; \
}
#define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \
{ \
static SEC_INFO_T info_s = { d_type, 0, sh_flags, \
sh_entsize}; \
info_s.align = ld_targ.t_m.m_word_align; \
sec_info = &info_s; \
}
switch (shtype) {
case SHT_PROGBITS:
/*
* SHT_PROGBITS sections contain are used for many
* different sections. Alignments and flags differ.
* Some have a standard entsize, and others don't.
* We set some defaults here, but there is no expectation
* that they are correct or complete for any specific
* purpose. The caller must provide the correct values.
*/
SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0)
break;
case SHT_SYMTAB:
SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym))
break;
case SHT_DYNSYM:
SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
break;
case SHT_SUNW_LDYNSYM:
ofl->ofl_flags |= FLG_OF_OSABI;
SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
break;
case SHT_STRTAB:
/*
* A string table may or may not be allocable, depending
* on context, so we leave that flag unset and leave it to
* the caller to add it if necessary.
*
* String tables do not have a standard entsize, so
* we set it to 0.
*/
SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0)
break;
case SHT_RELA:
/*
* Relocations with an addend (Everything except 32-bit X86).
* The caller is expected to set all section header flags.
*/
SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela))
break;
case SHT_REL:
/*
* Relocations without an addend (32-bit X86 only).
* The caller is expected to set all section header flags.
*/
SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel))
break;
case SHT_HASH:
SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
break;
case SHT_SUNW_symsort:
case SHT_SUNW_tlssort:
ofl->ofl_flags |= FLG_OF_OSABI;
SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
break;
case SHT_DYNAMIC:
/*
* A dynamic section may or may not be allocable, depending
* on context, so we leave that flag unset and leave it to
* the caller to add it if necessary.
*/
SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, SHF_WRITE, sizeof (Dyn))
break;
case SHT_NOBITS:
/*
* SHT_NOBITS is used for BSS-type sections. The size and
* alignment depend on the specific use and must be adjusted
* by the caller.
*/
SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0)
break;
case SHT_INIT_ARRAY:
case SHT_FINI_ARRAY:
case SHT_PREINIT_ARRAY:
SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE,
sizeof (Addr))
break;
case SHT_SYMTAB_SHNDX:
/*
* Note that these sections are created to be associated
* with both symtab and dynsym symbol tables. However, they
* are non-allocable in all cases, because the runtime
* linker has no need for this information. It is purely
* informational, used by elfdump(1), debuggers, etc.
*/
SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word));
break;
case SHT_SUNW_cap:
ofl->ofl_flags |= FLG_OF_OSABI;
SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap));
break;
case SHT_SUNW_move:
ofl->ofl_flags |= FLG_OF_OSABI;
/*
* The sh_info field of the SHT_*_syminfo section points
* to the header index of the associated .dynamic section,
* so we also set SHF_INFO_LINK.
*/
SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword),
SHF_ALLOC | SHF_WRITE, sizeof (Move));
break;
case SHT_SUNW_syminfo:
ofl->ofl_flags |= FLG_OF_OSABI;
/*
* The sh_info field of the SHT_*_syminfo section points
* to the header index of the associated .dynamic section,
* so we also set SHF_INFO_LINK.
*/
SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE,
SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo));
break;
case SHT_SUNW_verneed:
case SHT_SUNW_verdef:
ofl->ofl_flags |= FLG_OF_OSABI;
/*
* The info for verneed and versym happen to be the same.
* The entries in these sections are not of uniform size,
* so we set the entsize to 0.
*/
SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0);
break;
case SHT_SUNW_versym:
ofl->ofl_flags |= FLG_OF_OSABI;
SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC,
sizeof (Versym));
break;
default:
/* Should not happen: fcn called with unknown section type */
assert(0);
return (S_ERROR);
}
#undef SET_SEC_INFO
#undef SET_SEC_INFO_WORD_ALIGN
size = entcnt * sec_info->sh_entsize;
/*
* Allocate and initialize the Elf_Data structure.
*/
if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
return (S_ERROR);
data->d_type = sec_info->d_type;
data->d_size = size;
data->d_align = sec_info->align;
data->d_version = ofl->ofl_dehdr->e_version;
/*
* Allocate and initialize the Shdr structure.
*/
if ((shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
return (S_ERROR);
shdr->sh_type = shtype;
shdr->sh_size = size;
shdr->sh_flags = sec_info->sh_flags;
shdr->sh_addralign = sec_info->align;
shdr->sh_entsize = sec_info->sh_entsize;
/*
* Allocate and initialize the Is_desc structure.
*/
if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
return (S_ERROR);
isec->is_name = shname;
isec->is_shdr = shdr;
isec->is_indata = data;
*ret_isec = isec;
*ret_shdr = shdr;
*ret_data = data;
return (1);
}
/*
* Use an existing input section as a template to create a new
* input section with the same values as the original, other than
* the size of the data area which is supplied by the caller.
*
* entry:
* ofl - Output file descriptor
* ifl - Input file section to use as a template
* size - Size of data area for new section
* ret_isec, ret_shdr, ret_data - Address of pointers to
* receive address of newly allocated structs.
*
* exit:
* On error, returns S_ERROR. On success, returns (1), and the
* ret_ pointers have been updated to point at the new structures,
* which have been filled in. To finish the task, the caller must
* update any fields within the supplied descriptors that differ
* from its needs, and then call ld_place_section().
*/
static uintptr_t
new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size,
Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
{
Shdr *shdr;
Elf_Data *data;
Is_desc *isec;
/*
* Allocate and initialize the Elf_Data structure.
*/
if ((data = libld_calloc(sizeof (Elf_Data), 1)) == NULL)
return (S_ERROR);
data->d_type = tmpl_isp->is_indata->d_type;
data->d_size = size;
data->d_align = tmpl_isp->is_shdr->sh_addralign;
data->d_version = ofl->ofl_dehdr->e_version;
/*
* Allocate and initialize the Shdr structure.
*/
if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
return (S_ERROR);
*shdr = *tmpl_isp->is_shdr;
shdr->sh_addr = 0;
shdr->sh_offset = 0;
shdr->sh_size = size;
/*
* Allocate and initialize the Is_desc structure.
*/
if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
return (S_ERROR);
isec->is_name = tmpl_isp->is_name;
isec->is_shdr = shdr;
isec->is_indata = data;
*ret_isec = isec;
*ret_shdr = shdr;
*ret_data = data;
return (1);
}
/*
* Build a .bss section for allocation of tentative definitions. Any `static'
* .bss definitions would have been associated to their own .bss sections and
* thus collected from the input files. `global' .bss definitions are tagged
* as COMMON and do not cause any associated .bss section elements to be
* generated. Here we add up all these COMMON symbols and generate the .bss
* section required to represent them.
*/
uintptr_t
ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, uint_t ident)
{
Shdr *shdr;
Elf_Data *data;
Is_desc *isec;
Os_desc *osp;
Xword rsize = (Xword)ofl->ofl_relocbsssz;
/*
* Allocate header structs. We will set the name ourselves below,
* and there is no entcnt for a BSS. So, the shname and entcnt
* arguments are 0.
*/
if (new_section(ofl, SHT_NOBITS, NULL, 0,
&isec, &shdr, &data) == S_ERROR)
return (S_ERROR);
data->d_size = (size_t)size;
data->d_align = (size_t)align;
shdr->sh_size = size;
shdr->sh_addralign = align;
if (ident == ld_targ.t_id.id_tlsbss) {
isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
ofl->ofl_istlsbss = isec;
shdr->sh_flags |= SHF_TLS;
} else if (ident == ld_targ.t_id.id_bss) {
isec->is_name = MSG_ORIG(MSG_SCN_BSS);
ofl->ofl_isbss = isec;
#if defined(_ELF64)
} else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
(ident == ld_targ.t_id.id_lbss)) {
isec->is_name = MSG_ORIG(MSG_SCN_LBSS);
ofl->ofl_islbss = isec;
shdr->sh_flags |= SHF_AMD64_LARGE;
#endif
}
/*
* Retain this .*bss input section as this will be where global symbol
* references are added.
*/
if ((osp = ld_place_section(ofl, isec, ident, NULL)) ==
(Os_desc *)S_ERROR)
return (S_ERROR);
/*
* If relocations exist against a .*bss section, a section symbol must
* be created for the section in the .dynsym symbol table.
*/
if (!(osp->os_flags & FLG_OS_OUTREL)) {
ofl_flag_t flagtotest;
if (ident == ld_targ.t_id.id_tlsbss)
flagtotest = FLG_OF1_TLSOREL;
else
flagtotest = FLG_OF1_BSSOREL;
if (ofl->ofl_flags1 & flagtotest) {
ofl->ofl_dynshdrcnt++;
osp->os_flags |= FLG_OS_OUTREL;
}
}
osp->os_szoutrels = rsize;
return (1);
}
/*
* Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
* ld -z *array=name).
*/
static uintptr_t
make_array(Ofl_desc *ofl, Word shtype, const char *sectname, APlist *alp)
{
uint_t entcount;
Aliste idx;
Elf_Data *data;
Is_desc *isec;
Shdr *shdr;
Sym_desc *sdp;
Rel_desc reld;
Rela reloc;
Os_desc *osp;
uintptr_t ret = 1;
if (alp == NULL)
return (1);
entcount = 0;
for (APLIST_TRAVERSE(alp, idx, sdp))
entcount++;
if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
S_ERROR)
return (S_ERROR);
if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == NULL)
return (S_ERROR);
if (ld_place_section(ofl, isec, ld_targ.t_id.id_array, NULL) ==
(Os_desc *)S_ERROR)
return (S_ERROR);
osp = isec->is_osdesc;
if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY))
ofl->ofl_osinitarray = osp;
if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY))
ofl->ofl_ospreinitarray = osp;
else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY))
ofl->ofl_osfiniarray = osp;
/*
* Create relocations against this section to initialize it to the
* function addresses.
*/
reld.rel_osdesc = osp;
reld.rel_isdesc = isec;
reld.rel_move = 0;
reld.rel_flags = FLG_REL_LOAD;
/*
* Fabricate the relocation information (as if a relocation record had
* been input - see init_rel()).
*/
reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr;
reld.rel_roffset = 0;
reld.rel_raddend = 0;
reld.rel_typedata = 0;
/*
* Create a minimal relocation record to satisfy process_sym_reloc()
* debugging requirements.
*/
reloc.r_offset = 0;
reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr);
reloc.r_addend = 0;
DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp,
ld_targ.t_m.m_rel_sht_type));
for (APLIST_TRAVERSE(alp, idx, sdp)) {
reld.rel_sname = sdp->sd_name;
reld.rel_sym = sdp;
if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
MSG_INTL(MSG_STR_COMMAND), 0) == S_ERROR) {
ret = S_ERROR;
continue;
}
reld.rel_roffset += (Xword)sizeof (Addr);
reloc.r_offset = reld.rel_roffset;
}
return (ret);
}
/*
* Build a comment section (-Qy option).
*/
static uintptr_t
make_comment(Ofl_desc *ofl)
{
Shdr *shdr;
Elf_Data *data;
Is_desc *isec;
if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
&isec, &shdr, &data) == S_ERROR)
return (S_ERROR);
data->d_buf = (void *)ofl->ofl_sgsid;
data->d_size = strlen(ofl->ofl_sgsid) + 1;
data->d_align = 1;
shdr->sh_size = (Xword)data->d_size;
shdr->sh_flags = 0;
shdr->sh_addralign = 1;
return ((uintptr_t)ld_place_section(ofl, isec,
ld_targ.t_id.id_note, NULL));
}
/*
* Make the dynamic section. Calculate the size of any strings referenced
* within this structure, they will be added to the global string table
* (.dynstr). This routine should be called before make_dynstr().
*
* This routine must be maintained in parallel with update_odynamic()
* in update.c
*/
static uintptr_t
make_dynamic(Ofl_desc *ofl)
{
Shdr *shdr;
Os_desc *osp;
Elf_Data *data;
Is_desc *isec;
size_t cnt = 0;
Aliste idx;
Ifl_desc *ifl;
Sym_desc *sdp;
size_t size;
Str_tbl *strtbl;
ofl_flag_t flags = ofl->ofl_flags;
int not_relobj = !(flags & FLG_OF_RELOBJ);
int unused = 0;
/*
* Select the required string table.
*/
if (OFL_IS_STATIC_OBJ(ofl))
strtbl = ofl->ofl_strtab;
else
strtbl = ofl->ofl_dynstrtab;
/*
* Only a limited subset of DT_ entries apply to relocatable
* objects. See the comment at the head of update_odynamic() in
* update.c for details.
*/
if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
&isec, &shdr, &data) == S_ERROR)
return (S_ERROR);
/* new_section() does not set SHF_ALLOC. Add it if needed */
if (not_relobj)
shdr->sh_flags |= SHF_ALLOC;
osp = ofl->ofl_osdynamic =
ld_place_section(ofl, isec, ld_targ.t_id.id_dynamic, NULL);
/*
* Reserve entries for any needed dependencies.
*/
for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
continue;
/*
* If this dependency didn't satisfy any symbol references,
* generate a debugging diagnostic (ld(1) -Dunused can be used
* to display these). If this is a standard needed dependency,
* and -z ignore is in effect, drop the dependency. Explicitly
* defined dependencies (i.e., -N dep) don't get dropped, and
* are flagged as being required to simplify update_odynamic()
* processing.
*/
if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
if (unused++ == 0)
DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
(ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
if (ifl->ifl_flags & FLG_IF_NEEDSTR)
ifl->ifl_flags |= FLG_IF_DEPREQD;
else if (ifl->ifl_flags & FLG_IF_IGNORE)
continue;
}
/*
* If this object is a lazyload reserve a DT_POSFLAG_1 entry.
*/
if ((ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM)) &&
not_relobj)
cnt++;
if (st_insert(strtbl, ifl->ifl_soname) == -1)
return (S_ERROR);
cnt++;
/*
* If the needed entry contains the $ORIGIN token make sure
* the associated DT_1_FLAGS entry is created.
*/
if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
ofl->ofl_dtflags |= DF_ORIGIN;
}
}
if (unused)
DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
if (not_relobj) {
/*
* Reserve entries for any per-symbol auxiliary/filter strings.
*/
cnt += alist_nitems(ofl->ofl_dtsfltrs);
/*
* Reserve entries for _init() and _fini() section addresses.
*/