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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
|
#
# 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 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# ident "%Z%%M% %I% %E% SMI"
#
include $(SRC)/cmd/sgs/Makefile.sub
#
# 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 2010 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
#
PROG= elfwrap
include $(SRC)/cmd/Makefile.cmd
include $(SRC)/cmd/sgs/Makefile.com
COMOBJ = main.o
# Hammerhead: removed machine.sparc.o and machine.sparcv9.o (amd64-only)
MACHOBJ = machine.i386.o machine.amd64.o
COMOBJ32 = elfwrap32.o
COMOBJ64 = elfwrap64.o
SGSCOMMONOBJ = alist.o
BLTOBJ = msg.o
OBJS= $(BLTOBJ) $(COMOBJ) $(MACHOBJ) $(COMOBJ32) $(COMOBJ64) \
$(SGSCOMMONOBJ)
MAPFILES = $(MAPFILE.NGB)
MAPOPTS = $(MAPFILES:%=-Wl,-M%)
CPPFLAGS = -I. -I../common -I../../include $(CPPFLAGS.master) -I$(ELFCAP)
LLDFLAGS =
LLDFLAGS64 =
LDFLAGS += $(VERSREF) $(MAPOPTS) $(LLDFLAGS)
LDLIBS += $(ELFLIBDIR) -lelf $(CONVLIBDIR) -lconv
CERRWARN += $(CNOWARN_UNINIT)
BLTDEFS = msg.h
BLTDATA = msg.c
BLTMESG = $(SGSMSGDIR)/elfwrap
BLTFILES = $(BLTDEFS) $(BLTDATA) $(BLTMESG)
SGSMSGCOM = ../common/elfwrap.msg
SGSMSGTARG = $(SGSMSGCOM)
SGSMSGALL = $(SGSMSGCOM)
SGSMSGFLAGS += -h $(BLTDEFS) -d $(BLTDATA) -m $(BLTMESG) -n elfwrap_msg
SRCS = $(COMOBJ:%.o=../common/%.c) ../common/machine.c \
$(COMOBJ32:%32.o=../common/%.c) \
$(SGSCOMMONOBJ:%.o=$(SGSCOMMON)/%.c) $(BLTDATA)
CLEANFILES += $(BLTFILES)
#
# 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 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
#
%.o: ../common/%.c
$(COMPILE.c) -o $@ $<
$(POST_PROCESS_O)
%32.o: ../common/%.c
$(COMPILE.c) -o $@ $<
$(POST_PROCESS_O)
%64.o: ../common/%.c
$(COMPILE.c) -D_ELF64 -o $@ $<
$(POST_PROCESS_O)
%.o: $(SGSCOMMON)/%.c
$(COMPILE.c) -o $@ $<
$(POST_PROCESS_O)
# Hammerhead: removed %.sparc.o and %.sparcv9.o pattern rules (amd64-only)
%.i386.o: ../common/%.c
$(COMPILE.c) -DELFWRAP_X86 -o $@ $<
$(POST_PROCESS_O)
%.amd64.o: ../common/%.c
$(COMPILE.c) -DELFWRAP_X86 -D_ELF64 -o $@ $<
$(POST_PROCESS_O)
$(PROG): $(OBJS) $(MAPFILES)
$(LINK.c) -o $@ $(OBJS) $(LDLIBS)
$(POST_PROCESS)
all: $(PROG)
clean:
$(RM) $(OBJS) $(CLEANFILES)
install: all $(ROOTPROG)
include $(SRC)/cmd/Makefile.targ
# Derived source and header files (messaging).
catalog: $(BLTMESG)
chkmsg: $(SRCS)
sh $(CHKMSG) $(CHKMSGFLAGS) $(SRCS)
# Hammerhead: GNU Make grouped target syntax (&:) - was dmake + syntax
$(BLTDEFS) $(BLTDATA) $(BLTMESG) &: $(SGSMSGALL)
$(SGSMSG) $(SGSMSGFLAGS) $(SGSMSGALL)
#
# 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 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
#
include ../Makefile.com
.KEEP_STATE:
ELFLIBDIR = $(ELFLIBDIR64)
CONVLIBDIR = $(CONVLIBDIR64)
ROOTPROG= $(ROOTPROG64)
include ../Makefile.targ
include $(SRC)/Makefile.master.64
install: $(ROOTPROG64)
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef __ELFWRAP_H
#define __ELFWRAP_H
/*
* Local include file for elfwrap.
*/
#include <libelf.h>
#include <alist.h>
#include <_machelf.h>
/*
* Define a target descriptor to hold target specific information.
*/
typedef struct {
uchar_t td_class; /* target class (32-bit/64-bit) */
uchar_t td_data; /* target data (LSB/MSB) */
ushort_t td_mach; /* target machine (sparc, i386, etc.) */
size_t td_align; /* target data buffer alignment */
size_t td_symsz; /* target symbol table entry size */
} TargDesc_t;
/*
* Define a descriptor for each ELF section being output to the new file.
*/
typedef struct {
const char *os_name; /* section name */
Word os_type; /* section type */
Xword os_flags; /* section flags */
size_t os_ndx; /* section index (input file) */
off_t os_size; /* section size (input file) */
void *os_addr; /* section address (input file) */
Shdr *os_shdr; /* section header (output file) */
Elf_Data *os_data; /* section data (output file) */
} OutSec_t;
#define AL_CNT_WOSECS 10 /* default number of input sections */
/*
* Define a standard section descriptor.
*/
typedef struct {
const char *ss_name; /* section name */
Word ss_type; /* section type */
Xword ss_flags; /* section flags */
} StdSec_t;
/*
* Define a descriptor to maintain section information.
*/
typedef struct {
Alist *od_outsecs; /* list of output sections */
size_t od_symtabno; /* number of symbol table entries */
size_t od_strtabsz; /* string table size */
size_t od_shstrtabsz; /* section header string table size */
} ObjDesc_t;
/*
* Define all external interfaces.
*/
extern int input32(int, char **, const char *, const char *, ObjDesc_t *);
extern int input64(int, char **, const char *, const char *, ObjDesc_t *);
extern int output32(const char *, int, const char *, ushort_t,
ObjDesc_t *);
extern int output64(const char *, int, const char *, ushort_t,
ObjDesc_t *);
#if defined(lint)
extern void target_init(TargDesc_t *);
#else
extern void target_init_i386(TargDesc_t *);
extern void target_init_amd64(TargDesc_t *);
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* __ELFWRAP_H */
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <errno.h>
#include <libelf.h>
#include <stdio.h>
#include <strings.h>
#include <msg.h>
#include <machdep.h>
#include <_libelf.h>
#include <_elfwrap.h>
/*
* This module is compiled to support 32-bit and 64-bit class objects. Define
* the necessary interfaces for these classes.
*/
#if defined(_ELF64)
#define input input64
#define output output64
#else
#define input input32
#define output output32
#endif
static StdSec_t StdSecs[] = {
{ MSG_ORIG(MSG_SCN_SYMTAB), SHT_SYMTAB, 0 },
{ MSG_ORIG(MSG_SCN_STRTAB), SHT_STRTAB, SHF_STRINGS},
{ MSG_ORIG(MSG_SCN_SHSTRTAB), SHT_STRTAB, SHF_STRINGS},
{ NULL, 0, 0 }
};
/*
* Process all input files. These contain the data that will be assigned to a
* new ELF section.
*/
int
input(int argc, char **argv, const char *prog, const char *ofile,
ObjDesc_t *odp)
{
OutSec_t outsec;
StdSec_t *stdsecs;
size_t ndx, cnt;
int ret = 0, fd = -1;
/*
* Make sure we have access to read each input file, and prepare an
* output section descriptor for each. Note, we assign section indexes
* starting at 1, as section index 0 is special, and is created by
* libelf.
*/
for (ndx = 1; argc; argc--, argv++, ndx++) {
char *file = *argv;
struct stat status;
size_t namesz;
/*
* Close any previously opened file.
*/
if (fd != -1)
(void) close(fd);
/*
* Identify the section.
*/
outsec.os_name = basename(file);
outsec.os_type = SHT_PROGBITS;
outsec.os_flags = SHF_ALLOC;
outsec.os_ndx = ndx;
if ((fd = open(file, O_RDONLY)) == -1) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_ERR_OPEN),
prog, file, strerror(err));
ret = 1;
continue;
}
if (fstat(fd, &status) == -1) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_ERR_FSTAT),
prog, file, strerror(err));
ret = 1;
continue;
}
if ((outsec.os_size = status.st_size) == 0) {
(void) fprintf(stderr, MSG_INTL(MSG_WARN_ZERO),
prog, file);
continue;
}
if ((outsec.os_addr = mmap(0, outsec.os_size, PROT_READ,
MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_ERR_MMAP),
prog, file, strerror(err));
ret = 1;
continue;
}
if (alist_append(&(odp->od_outsecs), &outsec, sizeof (OutSec_t),
AL_CNT_WOSECS) == 0) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_ERR_ALLOC),
prog, file, strerror(err));
return (1);
}
/*
* Each data section contributes:
*
* i. its basename, prefixed with a "dot", to the .shstrtab.
* ii. a section symbol.
* iii. a data symbol, using the basename, with an
* appended "_data" string.
* iv. a data size symbol, using the basename with an
* appended "_size" string.
*/
namesz = strlen(outsec.os_name) + 1;
odp->od_symtabno += 3;
odp->od_strtabsz += (namesz + MSG_STR_START_SIZE);
odp->od_strtabsz += (namesz + MSG_STR_END_SIZE);
odp->od_shstrtabsz += (namesz + MSG_STR_DOT_SIZE);
}
if (fd != -1)
(void) close(fd);
/*
* If an error occurred, or no input files contributed data, bail now.
*/
if (ret || (odp->od_outsecs == NULL))
return (1);
/*
* Create section descriptors for .symtab, .strtab, and .shstrtab.
*/
for (cnt = 0, stdsecs = &StdSecs[cnt]; stdsecs->ss_name; cnt++,
ndx++, stdsecs = &StdSecs[cnt]) {
/*
* Identify the section.
*/
outsec.os_name = stdsecs->ss_name;
outsec.os_type = stdsecs->ss_type;
outsec.os_flags = stdsecs->ss_flags;
outsec.os_ndx = ndx;
outsec.os_size = 0;
outsec.os_addr = 0;
if (alist_append(&(odp->od_outsecs), &outsec, sizeof (OutSec_t),
AL_CNT_WOSECS) == 0) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_ERR_ALLOC),
prog, outsec.os_name, strerror(err));
return (1);
}
/*
* Each standard section contributes:
*
* i. its section name to the .shstrtab.
* ii. a section symbol.
*/
odp->od_symtabno++;
odp->od_shstrtabsz += (strlen(outsec.os_name) + 1);
}
/*
* The symbol table requires an initial NULL entry and a following
* FILE entry. Both string tables require an initial NULL byte.
* The .strtab requires room for the output file name (STT_FILE).
*/
odp->od_symtabno += 2;
odp->od_strtabsz += strlen(ofile) + 2;
odp->od_shstrtabsz++;
return (0);
}
/*
* Having captured all input data, create the output file.
*/
int
output(const char *prog, int fd, const char *ofile, ushort_t mach,
ObjDesc_t *odp)
{
Aliste off;
Elf *melf, *oelf;
Ehdr *ehdr;
Sym *symtab, *secsymtabent, *glbsymtabent;
char *strtab, *strtabent, *shstrtab, *shstrtabent;
OutSec_t *outsec, *outsymtab, *outstrtab, *outshstrtab;
size_t len;
TargDesc_t tdesc;
/*
* Obtain any target specific ELF information.
*/
if (mach == 0)
mach = M_MACH;
switch (mach) {
#if !defined(lint)
case EM_386:
target_init_i386(&tdesc);
break;
case EM_AMD64:
target_init_amd64(&tdesc);
break;
#else
default:
target_init(&tdesc);
break;
#endif
}
/*
* Create a new ELF descriptor for the new output file.
*/
if ((oelf = elf_begin(fd, ELF_C_WRITE, 0)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_BEGIN), prog,
elf_errmsg(elf_errno()));
return (1);
}
/*
* Create and initialize the new ELF header.
*/
if ((ehdr = elf_newehdr(oelf)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_NEWEHDR), prog,
elf_errmsg(elf_errno()));
return (1);
}
/*
* Note, the ELF header is initialized to reflect the host running
* elfwrap(1) rather than the target. Using host byte order allows
* elfwrap(1) to create the object data. Prior to the final update,
* the output ELF header is modified to reflect the target, causing
* libelf to produce the output object using the correct byte order
* and other target information.
*/
ehdr->e_ident[EI_DATA] = M_DATA;
ehdr->e_type = ET_REL;
ehdr->e_version = EV_CURRENT;
/*
* Create the required number of new sections, their associated section
* header, and an initial data buffer.
*/
for (ALIST_TRAVERSE(odp->od_outsecs, off, outsec)) {
Elf_Scn *scn;
Elf_Data *data;
Shdr *shdr;
if ((scn = elf_newscn(oelf)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_NEWSCN),
prog, outsec->os_name, elf_errmsg(elf_errno()));
return (1);
}
if ((shdr = elf_getshdr(scn)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETSHDR),
prog, outsec->os_name, elf_errmsg(elf_errno()));
return (1);
}
/*
* Assign the section type and flags.
*/
shdr->sh_type = outsec->os_type;
shdr->sh_flags = outsec->os_flags;
if ((data = elf_newdata(scn)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_NEWDATA),
prog, outsec->os_name, elf_errmsg(elf_errno()));
return (1);
}
switch (shdr->sh_type) {
case SHT_PROGBITS:
/*
* If this is a PROGBITS section, then the data
* originates from an input file. Assign the data
* buffer to this input file and provide a default
* alignment.
*/
data->d_buf = outsec->os_addr;
data->d_type = ELF_T_BYTE;
data->d_size = outsec->os_size;
data->d_align = tdesc.td_align;
break;
case SHT_SYMTAB:
/*
* If this is the symbol table, use the symbol count to
* reserve sufficient space for the symbols we need.
*/
data->d_buf = 0;
data->d_type = ELF_T_SYM;
data->d_size = (odp->od_symtabno * tdesc.td_symsz);
data->d_align = tdesc.td_align;
break;
case SHT_STRTAB:
/*
* If this is a string table, use the table size to
* reserve sufficient space for the strings we need.
*/
data->d_buf = 0;
data->d_type = ELF_T_BYTE;
if (strcmp(outsec->os_name, MSG_ORIG(MSG_SCN_STRTAB)))
data->d_size = odp->od_shstrtabsz;
else
data->d_size = odp->od_strtabsz;
data->d_align = 1;
break;
}
}
/*
* Write the ELF data into a memory image.
*/
if ((elf_update(oelf, ELF_C_WRIMAGE)) == -1) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_UPDATE), prog,
elf_errmsg(elf_errno()));
return (1);
}
/*
* Assign an ELF descriptor to the memory image.
*/
if ((melf = elf_begin(0, ELF_C_IMAGE, oelf)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_BEGIN), prog,
elf_errmsg(elf_errno()));
return (1);
}
/*
* Get the ELF header from the memory image.
*/
if ((ehdr = elf_getehdr(melf)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR), prog,
elf_errmsg(elf_errno()));
return (1);
}
/*
* Read the section header and data from the new sections of the
* memory image.
*/
for (ALIST_TRAVERSE(odp->od_outsecs, off, outsec)) {
Elf_Scn *scn;
Shdr *shdr;
if ((scn = elf_getscn(melf, outsec->os_ndx)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETSCN),
prog, outsec->os_name, elf_errmsg(elf_errno()));
return (1);
}
if ((outsec->os_shdr = shdr = elf_getshdr(scn)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETSHDR),
prog, outsec->os_name, elf_errmsg(elf_errno()));
return (1);
}
if ((outsec->os_data = elf_getdata(scn, NULL)) == NULL) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETDATA),
prog, outsec->os_name, elf_errmsg(elf_errno()));
return (1);
}
if (shdr->sh_type == SHT_PROGBITS)
continue;
/*
* Remember the symbol table and string tables, so that they
* can be filled in later.
*/
if (shdr->sh_type == SHT_SYMTAB) {
outsymtab = outsec;
symtab = (Sym *)outsec->os_data->d_buf;
} else if (shdr->sh_type == SHT_STRTAB) {
if (strcmp(outsec->os_name, MSG_ORIG(MSG_SCN_STRTAB))) {
outshstrtab = outsec;
shstrtab = (char *)outsec->os_data->d_buf;
} else {
outstrtab = outsec;
strtab = (char *)outsec->os_data->d_buf;
}
}
}
/*
* Update the ELF header with the .shstrtab index.
*/
ehdr->e_shstrndx = outshstrtab->os_ndx;
/*
* Set up the string table entries, and skip the first byte.
*/
strtabent = strtab;
strtabent++;
shstrtabent = shstrtab;
shstrtabent++;
/*
* Skip the first symbol table entry. Write a FILE entry, and set
* up for adding sections and data symbols. Associate the symbol
* table with the string table.
*/
secsymtabent = symtab;
secsymtabent++;
secsymtabent->st_name = (strtabent - strtab);
secsymtabent->st_info = ELF_ST_INFO(STB_LOCAL, STT_NOTYPE);
secsymtabent->st_shndx = SHN_ABS;
secsymtabent++;
glbsymtabent = secsymtabent;
glbsymtabent += alist_nitems(odp->od_outsecs);
outsymtab->os_shdr->sh_link = outstrtab->os_ndx;
/*
* Write the output file name to the .strtab.
*/
len = strlen(ofile) + 1;
(void) memcpy(strtabent, ofile, len);
strtabent += len;
/*
* Rescan all the new sections, adding symbols and strings as required.
*/
for (ALIST_TRAVERSE(odp->od_outsecs, off, outsec)) {
size_t alen;
/*
* Create a section symbol.
*/
secsymtabent->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
secsymtabent->st_shndx = outsec->os_ndx;
secsymtabent++;
/*
* Store the section name, (with an appended "." if the section
* name is derived from the input file name), and point the
* section header to this name.
*/
outsec->os_shdr->sh_name = (shstrtabent - shstrtab);
if (outsec->os_shdr->sh_type == SHT_PROGBITS) {
(void) memcpy(shstrtabent, MSG_ORIG(MSG_STR_DOT),
MSG_STR_DOT_SIZE);
shstrtabent += MSG_STR_DOT_SIZE;
}
len = strlen(outsec->os_name) + 1;
(void) memcpy(shstrtabent, outsec->os_name, len);
shstrtabent += len;
if (outsec->os_shdr->sh_type != SHT_PROGBITS)
continue;
/*
* Add a symbol pointing to this PROGBITS section. The value
* is the base offset of this section, which can only be 0.
* The size of the symbol can be taken straight from the section
* header information (that libelf generated).
*/
glbsymtabent->st_name = (strtabent - strtab);
glbsymtabent->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
glbsymtabent->st_shndx = outsec->os_ndx;
glbsymtabent->st_size = outsec->os_shdr->sh_size;
glbsymtabent++;
/*
* Store this symbol name (with an appended "_data") in the
* string table.
*/
len--;
(void) memcpy(strtabent, outsec->os_name, len);
strtabent += len;
alen = (MSG_STR_START_SIZE + 1);
(void) memcpy(strtabent, MSG_ORIG(MSG_STR_START), alen);
strtabent += alen;
/*
* Add a symbol indicating the size of this PROGBITS section.
*/
glbsymtabent->st_name = (strtabent - strtab);
glbsymtabent->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
glbsymtabent->st_shndx = outsec->os_ndx;
glbsymtabent->st_value = outsec->os_shdr->sh_size;
glbsymtabent++;
/*
* Store this symbol name (with an appended "_end") in the
* string table.
*/
(void) memcpy(strtabent, outsec->os_name, len);
strtabent += len;
alen = (MSG_STR_END_SIZE + 1);
(void) memcpy(strtabent, MSG_ORIG(MSG_STR_END), alen);
strtabent += alen;
}
/*
* Update the .symtab section header with the index of the first
* non-local symbol. The only locals written are the section symbols.
*/
outsymtab->os_shdr->sh_info = (secsymtabent - symtab);
/*
* Having updated the image following the byte order of elfwrap(), seed
* the ELF header with the appropriate target information.
*/
ehdr->e_ident[EI_CLASS] = tdesc.td_class;
ehdr->e_ident[EI_DATA] = tdesc.td_data;
ehdr->e_machine = tdesc.td_mach;
/*
* If the output relocatable object is targeted to a machine with a
* different byte order than the host running elfwrap(1), swap the data
* to the target byte order.
*/
if ((_elf_sys_encoding() != ehdr->e_ident[EI_DATA]) &&
(_elf_swap_wrimage(melf) != 0)) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_SWAP_WRIMAGE), prog,
elf_errmsg(elf_errno()));
return (1);
}
(void) elf_end(melf);
/*
* Finally, write the updated memory image out to disc.
*/
if ((elf_update(oelf, ELF_C_WRITE)) == -1) {
(void) fprintf(stderr, MSG_INTL(MSG_ELF_UPDATE), prog,
elf_errmsg(elf_errno()));
return (1);
}
(void) elf_end(oelf);
return (0);
}
#
# 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 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# ident "%Z%%M% %I% %E% SMI"
#
@ _START_
# Message file for cmd/sgs/elfwrap.
@ MSG_ID_ELFWRAP
# Usage Messages
@ MSG_USAGE_BRIEF "usage: %s [-64] [-o file] [-z target=platform] \
data, ...\n"
@ MSG_ARG_BADTARG "%s: unknown target platform: %s\n"
@ MSG_ARG_ILLEGAL "%s: option %s has illegal argument `%s'\n"
@ MSG_ERR_LIBELF "%s: libelf: version not supported: %d\n"
@ MSG_ERR_ALLOC "%s: %s: can't allocate memory: %s\n"
@ MSG_ERR_FSTAT "%s: %s: can't fstat: %s\n"
@ MSG_ERR_MMAP "%s: %s: can't mmap: %s\n"
@ MSG_ERR_OPEN "%s: %s: can't open: %s\n"
@ MSG_WARN_ZERO "%s: %s: has zero length - ignoring\n"
@ MSG_ELF_BEGIN "%s: elf_begin failed: %s\n"
@ MSG_ELF_GETEHDR "%s: elf_getehdr failed: %s\n"
@ MSG_ELF_NEWEHDR "%s: elf_newehdr failed: %s\n"
@ MSG_ELF_SWAP_WRIMAGE "%s: elf_swap_wrimage failed: %s\n"
@ MSG_ELF_UPDATE "%s: elf_update failed: %s\n"
@ MSG_ELF_GETDATA "%s: %s: elf_getdata failed: %s\n"
@ MSG_ELF_GETSCN "%s: %s: elf_getscn failed: %s\n"
@ MSG_ELF_GETSHDR "%s: %s: elf_getshdr failed: %s\n"
@ MSG_ELF_NEWDATA "%s: %s: elf_newdata failed: %s\n"
@ MSG_ELF_NEWSCN "%s: %s: elf_newscn failed: %s\n"
@ _END_
# The following strings represent reserved words, files, path names and symbols.
# Reference to this strings is via the MSG_ORIG() macro, and thus no message
# translation is required.
@ MSG_SUNW_OST_SGS "SUNW_OST_SGS"
@ MSG_ARG_6 "-6"
@ MSG_ARG_OPTIONS "6:o:z:"
@ MSG_ARG_TARGET "target="
@ MSG_ARG_Z "-z"
@ MSG_TARG_SPARC "sparc"
@ MSG_TARG_X86 "x86"
@ MSG_STR_AWRAPO "a.wrap.o"
@ MSG_STR_EMPTY ""
@ MSG_STR_START "_start"
@ MSG_STR_END "_end"
@ MSG_STR_DOT "."
@ MSG_SCN_SYMTAB ".symtab"
@ MSG_SCN_STRTAB ".strtab"
@ MSG_SCN_SHSTRTAB ".shstrtab"
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <_elfwrap.h>
#if defined(lint)
#include <machdep.h>
#else
#if defined(ELFWRAP_X86)
#include <i386/machdep_x86.h>
#if defined(_ELF64)
#define target_init target_init_amd64
#else
#define target_init target_init_i386
#endif
#endif
#endif
/*
* Establish any target specific data. This module is compiled using the
* defines shown above, to provide data for each target machine elfwrap(1)
* supports - each target module being assigned a unique interface name.
*/
void
target_init(TargDesc_t *tdp)
{
/*
* ELF header information.
*/
tdp->td_class = M_CLASS; /* e_ident[EI_CLASS] */
tdp->td_data = M_DATA; /* e_ident[EI_DATA] */
tdp->td_mach = M_MACH; /* e_machine */
/*
* Default data buffer alignment.
*/
tdp->td_align = M_WORD_ALIGN; /* d_align */
/*
* Symbol table entry size.
*/
tdp->td_symsz = sizeof (Sym); /* d_size */
}
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Wrap data in an elf file.
*/
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <libintl.h>
#include <conv.h>
#include <msg.h>
#include <_elfwrap.h>
const char *
_elfwrap_msg(Msg mid)
{
return (gettext(MSG_ORIG(mid)));
}
int
main(int argc, char **argv, char **envp)
{
const char *prog, *ofile = NULL, *pstr = NULL;
int fd, var;
uchar_t class = ELFCLASS32;
ushort_t mach = EM_NONE;
ObjDesc_t odesc = { NULL, 0, 0, 0 };
/*
* If we're on a 64-bit kernel, try to exec a full 64-bit version of
* the binary. If successful, conv_check_native() won't return.
*/
(void) conv_check_native(argv, envp);
/*
* Establish locale.
*/
(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
(void) setvbuf(stdout, NULL, _IOLBF, 0);
(void) setvbuf(stderr, NULL, _IOLBF, 0);
prog = basename(argv[0]);
opterr = 0;
while ((var = getopt(argc, argv, MSG_ORIG(MSG_ARG_OPTIONS))) != EOF) {
switch (var) {
case '6': /* Create a 64-bit object */
if (optarg[0] != '4') {
(void) fprintf(stderr,
MSG_INTL(MSG_ARG_ILLEGAL), prog,
MSG_ORIG(MSG_ARG_6), optarg);
return (1);
}
class = ELFCLASS64;
break;
case 'o': /* output file name */
ofile = optarg;
break;
case 'z': /* output file platform */
if (strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
MSG_ARG_TARGET_SIZE) == 0)
pstr = optarg + MSG_ARG_TARGET_SIZE;
else {
(void) fprintf(stderr,
MSG_INTL(MSG_ARG_ILLEGAL), prog,
MSG_ORIG(MSG_ARG_Z), optarg);
return (1);
}
break;
case '?':
(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF),
prog);
return (1);
default:
break;
}
}
/*
* Verify that we have at least one input data file, and if no output
* file has been specified, provide a default. Update argc and argv
* for input() to continue processing any input files.
*/
argv += optind;
argc -= optind;
if (argc == 0) {
(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF), prog);
return (1);
}
if (ofile == NULL)
ofile = MSG_ORIG(MSG_STR_AWRAPO);
/*
* If the user specified a target, use it to determine the machine type
* for the output object. If no target is specified, we leave "mach" as
* EM_NONE. output() will replace EM_NONE with the appropriate machine
* code for the system running elfwrap(1).
*/
if (pstr) {
if (strcasecmp(pstr, MSG_ORIG(MSG_TARG_SPARC)) == 0) {
if (class == ELFCLASS64)
mach = EM_SPARCV9;
else
mach = EM_SPARC;
} else if (strcasecmp(pstr, MSG_ORIG(MSG_TARG_X86)) == 0) {
if (class == ELFCLASS64)
mach = EM_AMD64;
else
mach = EM_386;
} else {
(void) fprintf(stderr, MSG_INTL(MSG_ARG_BADTARG), prog,
pstr);
return (1);
}
}
/*
* Create the input information for the new image.
*/
if (class == ELFCLASS64) {
if (input64(argc, argv, prog, ofile, &odesc) == 1)
return (1);
} else {
if (input32(argc, argv, prog, ofile, &odesc) == 1)
return (1);
}
/*
* Create and truncate the output file.
*/
if ((fd = open(ofile, (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_ERR_OPEN), prog,
ofile, strerror(err));
return (1);
}
/*
* Initialize libelf, and create the new ELF file as the class dictates.
*/
if (elf_version(EV_CURRENT) == EV_NONE) {
(void) fprintf(stderr, MSG_INTL(MSG_ERR_LIBELF), prog,
EV_CURRENT);
return (1);
}
if (class == ELFCLASS64)
return (output64(prog, fd, ofile, mach, &odesc));
else
return (output32(prog, fd, ofile, mach, &odesc));
}
|