View | Details | Raw Unified | Return to bug 207091 | Differences between
and this patch

Collapse All | Expand All

(-)b/Makefile.inc1 (+2 lines)
Lines 1638-1643 _binutils= gnu/usr.bin/binutils Link Here
1638
.endif
1638
.endif
1639
.if ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
1639
.if ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
1640
_elftctools=	lib/libelftc \
1640
_elftctools=	lib/libelftc \
1641
		lib/libpe \
1641
		usr.bin/elfcopy \
1642
		usr.bin/elfcopy \
1642
		usr.bin/nm \
1643
		usr.bin/nm \
1643
		usr.bin/size \
1644
		usr.bin/size \
Lines 1650-1655 _elftctools+= usr.bin/addr2line Link Here
1650
# If cross-building with an external binutils we still need to build strip for
1651
# If cross-building with an external binutils we still need to build strip for
1651
# the target (for at least crunchide).
1652
# the target (for at least crunchide).
1652
_elftctools=	lib/libelftc \
1653
_elftctools=	lib/libelftc \
1654
		lib/libpe \
1653
		usr.bin/elfcopy
1655
		usr.bin/elfcopy
1654
.endif
1656
.endif
1655
1657
(-)b/contrib/elftoolchain/addr2line/addr2line.c (-3 / +5 lines)
Lines 40-46 Link Here
40
#include "uthash.h"
40
#include "uthash.h"
41
#include "_elftc.h"
41
#include "_elftc.h"
42
42
43
ELFTC_VCSID("$Id: addr2line.c 3264 2015-11-30 05:38:14Z kaiwang27 $");
43
ELFTC_VCSID("$Id: addr2line.c 3273 2015-12-11 21:38:57Z kaiwang27 $");
44
44
45
struct Func {
45
struct Func {
46
	char *name;
46
	char *name;
Lines 368-374 print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file, Link Here
368
				printf("%s\n", f->name);
368
				printf("%s\n", f->name);
369
		}
369
		}
370
	}
370
	}
371
	(void) printf("%s:%ju\n", base ? basename(file) : file, call_line);
371
	(void) printf("%s:%ju\n", base ? basename(file) : file,
372
	    (uintmax_t) call_line);
372
373
373
	if (f->inlined_caller != NULL)
374
	if (f->inlined_caller != NULL)
374
		print_inlines(cu, f->inlined_caller, f->call_file,
375
		print_inlines(cu, f->inlined_caller, f->call_file,
Lines 562-568 out: Link Here
562
		}
563
		}
563
	}
564
	}
564
565
565
	(void) printf("%s:%ju\n", base ? basename(file) : file, lineno);
566
	(void) printf("%s:%ju\n", base ? basename(file) : file,
567
	    (uintmax_t) lineno);
566
568
567
	if (ret == DW_DLV_OK && inlines && cu != NULL &&
569
	if (ret == DW_DLV_OK && inlines && cu != NULL &&
568
	    cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL)
570
	    cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL)
(-)b/contrib/elftoolchain/addr2line/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/ar/ar.c (-2 / +2 lines)
Lines 72-78 Link Here
72
72
73
#include "ar.h"
73
#include "ar.h"
74
74
75
ELFTC_VCSID("$Id: ar.c 3243 2015-08-31 19:28:45Z emaste $");
75
ELFTC_VCSID("$Id: ar.c 3319 2016-01-13 21:37:53Z jkoshy $");
76
76
77
enum options
77
enum options
78
{
78
{
Lines 407-413 Usage: %s <command> [options] archive file...\n\ Link Here
407
  -F FORMAT | --flavor=FORMAT\n\
407
  -F FORMAT | --flavor=FORMAT\n\
408
                Create archives with the specified format.\n\
408
                Create archives with the specified format.\n\
409
  -S            Do not generate an archive symbol table.\n\
409
  -S            Do not generate an archive symbol table.\n\
410
  -U            Use original metadata, for unique archive checksums.\n"
410
  -U            Use original metadata for archive members.\n"
411
411
412
static void
412
static void
413
bsdar_usage(void)
413
bsdar_usage(void)
(-)b/contrib/elftoolchain/brandelf/brandelf.c (-2 / +2 lines)
Lines 44-50 Link Here
44
44
45
#include "_elftc.h"
45
#include "_elftc.h"
46
46
47
ELFTC_VCSID("$Id: brandelf.c 3234 2015-07-31 12:35:09Z emaste $");
47
ELFTC_VCSID("$Id: brandelf.c 3354 2016-01-18 21:50:15Z jkoshy $");
48
48
49
static int elftype(const char *);
49
static int elftype(const char *);
50
static const char *iselftype(int);
50
static const char *iselftype(int);
Lines 212-218 main(int argc, char **argv) Link Here
212
			/*
212
			/*
213
			 * Update the ABI type.
213
			 * Update the ABI type.
214
			 */
214
			 */
215
			ehdr.e_ident[EI_OSABI] = type;
215
			ehdr.e_ident[EI_OSABI] = (unsigned char) type;
216
			if (gelf_update_ehdr(elf, &ehdr) == 0) {
216
			if (gelf_update_ehdr(elf, &ehdr) == 0) {
217
				warnx("gelf_update_ehdr error: %s",
217
				warnx("gelf_update_ehdr error: %s",
218
				    elf_errmsg(-1));
218
				    elf_errmsg(-1));
(-)b/contrib/elftoolchain/common/elfdefinitions.h (-8 / +131 lines)
Lines 23-29 Link Here
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
24
 * SUCH DAMAGE.
25
 *
25
 *
26
 * $Id: elfdefinitions.h 3253 2015-10-10 18:31:33Z kaiwang27 $
26
 * $Id: elfdefinitions.h 3392 2016-02-05 19:51:22Z emaste $
27
 */
27
 */
28
28
29
/*
29
/*
Lines 1228-1233 _ELF_DEFINE_STB(STB_GLOBAL, 1, \ Link Here
1228
_ELF_DEFINE_STB(STB_WEAK,            2,					\
1228
_ELF_DEFINE_STB(STB_WEAK,            2,					\
1229
	"visible across all object files but with low precedence")	\
1229
	"visible across all object files but with low precedence")	\
1230
_ELF_DEFINE_STB(STB_LOOS,            10, "start of OS-specific range")	\
1230
_ELF_DEFINE_STB(STB_LOOS,            10, "start of OS-specific range")	\
1231
_ELF_DEFINE_STB(STB_GNU_UNIQUE,      10, "unique symbol (GNU)")		\
1231
_ELF_DEFINE_STB(STB_HIOS,            12, "end of OS-specific range")	\
1232
_ELF_DEFINE_STB(STB_HIOS,            12, "end of OS-specific range")	\
1232
_ELF_DEFINE_STB(STB_LOPROC,          13,				\
1233
_ELF_DEFINE_STB(STB_LOPROC,          13,				\
1233
	"start of processor-specific range")				\
1234
	"start of processor-specific range")				\
Lines 1259-1264 _ELF_DEFINE_STT(STT_LOPROC, 13, \ Link Here
1259
	"start of processor-specific types")				\
1260
	"start of processor-specific types")				\
1260
_ELF_DEFINE_STT(STT_ARM_TFUNC,       13, "Thumb function (GNU)")	\
1261
_ELF_DEFINE_STT(STT_ARM_TFUNC,       13, "Thumb function (GNU)")	\
1261
_ELF_DEFINE_STT(STT_ARM_16BIT,       15, "Thumb label (GNU)")		\
1262
_ELF_DEFINE_STT(STT_ARM_16BIT,       15, "Thumb label (GNU)")		\
1263
_ELF_DEFINE_STT(STT_SPARC_REGISTER,  13, "SPARC register information")	\
1262
_ELF_DEFINE_STT(STT_HIPROC,          15,				\
1264
_ELF_DEFINE_STT(STT_HIPROC,          15,				\
1263
	"end of processor-specific types")
1265
	"end of processor-specific types")
1264
1266
Lines 1395-1401 _ELF_DEFINE_RELOC(R_386_GOT32, 3) \ Link Here
1395
_ELF_DEFINE_RELOC(R_386_PLT32,		4)	\
1397
_ELF_DEFINE_RELOC(R_386_PLT32,		4)	\
1396
_ELF_DEFINE_RELOC(R_386_COPY,		5)	\
1398
_ELF_DEFINE_RELOC(R_386_COPY,		5)	\
1397
_ELF_DEFINE_RELOC(R_386_GLOB_DAT,	6)	\
1399
_ELF_DEFINE_RELOC(R_386_GLOB_DAT,	6)	\
1398
_ELF_DEFINE_RELOC(R_386_JMP_SLOT,	7)	\
1400
_ELF_DEFINE_RELOC(R_386_JUMP_SLOT,	7)	\
1399
_ELF_DEFINE_RELOC(R_386_RELATIVE,	8)	\
1401
_ELF_DEFINE_RELOC(R_386_RELATIVE,	8)	\
1400
_ELF_DEFINE_RELOC(R_386_GOTOFF,		9)	\
1402
_ELF_DEFINE_RELOC(R_386_GOTOFF,		9)	\
1401
_ELF_DEFINE_RELOC(R_386_GOTPC,		10)	\
1403
_ELF_DEFINE_RELOC(R_386_GOTPC,		10)	\
Lines 1407-1415 _ELF_DEFINE_RELOC(R_386_PC8, 23) Link Here
1407
1409
1408
/*
1410
/*
1409
 */
1411
 */
1410
#define	_ELF_DEFINE_AARCH64_RELOCATIONS()		\
1412
#define	_ELF_DEFINE_AARCH64_RELOCATIONS()				\
1411
_ELF_DEFINE_RELOC(R_AARCH64_ABS64,		257)	\
1413
_ELF_DEFINE_RELOC(R_AARCH64_NONE,				0)	\
1412
_ELF_DEFINE_RELOC(R_AARCH64_ABS32,		258)	\
1414
_ELF_DEFINE_RELOC(R_AARCH64_ABS64,				257)	\
1415
_ELF_DEFINE_RELOC(R_AARCH64_ABS32,				258)	\
1416
_ELF_DEFINE_RELOC(R_AARCH64_ABS16,				259)	\
1417
_ELF_DEFINE_RELOC(R_AARCH64_PREL64,				260)	\
1418
_ELF_DEFINE_RELOC(R_AARCH64_PREL32,				261)	\
1419
_ELF_DEFINE_RELOC(R_AARCH64_PREL16,				262)	\
1420
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_UABS_G0,			263)	\
1421
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_UABS_G0_NC,			264)	\
1422
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_UABS_G1,			265)	\
1423
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_UABS_G1_NC,			266)	\
1424
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_UABS_G2,			267)	\
1425
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_UABS_G2_NC,			268)	\
1426
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_UABS_G3,			269)	\
1427
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_SABS_G0,			270)	\
1428
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_SABS_G1,			271)	\
1429
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_SABS_G2,			272)	\
1430
_ELF_DEFINE_RELOC(R_AARCH64_LD_PREL_LO19,			273)	\
1431
_ELF_DEFINE_RELOC(R_AARCH64_ADR_PREL_LO21,			274)	\
1432
_ELF_DEFINE_RELOC(R_AARCH64_ADR_PREL_PG_HI21,			275)	\
1433
_ELF_DEFINE_RELOC(R_AARCH64_ADR_PREL_PG_HI21_NC,		276)	\
1434
_ELF_DEFINE_RELOC(R_AARCH64_ADD_ABS_LO12_NC,			277)	\
1435
_ELF_DEFINE_RELOC(R_AARCH64_LDST8_ABS_LO12_NC,			278)	\
1436
_ELF_DEFINE_RELOC(R_AARCH64_TSTBR14,				279)	\
1437
_ELF_DEFINE_RELOC(R_AARCH64_CONDBR19,				280)	\
1438
_ELF_DEFINE_RELOC(R_AARCH64_JUMP26,				282)	\
1439
_ELF_DEFINE_RELOC(R_AARCH64_CALL26,				283)	\
1440
_ELF_DEFINE_RELOC(R_AARCH64_LDST16_ABS_LO12_NC,			284)	\
1441
_ELF_DEFINE_RELOC(R_AARCH64_LDST32_ABS_LO12_NC,			285)	\
1442
_ELF_DEFINE_RELOC(R_AARCH64_LDST64_ABS_LO12_NC,			286)	\
1443
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_PREL_G0,			287)	\
1444
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_PREL_G0_NC,			288)	\
1445
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_PREL_G1,			289)	\
1446
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_PREL_G1_NC,			290)	\
1447
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_PREL_G2,			291)	\
1448
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_PREL_G2_NC,			292)	\
1449
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_PREL_G3,			293)	\
1450
_ELF_DEFINE_RELOC(R_AARCH64_LDST128_ABS_LO12_NC,		299)	\
1451
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_GOTOFF_G0,			300)	\
1452
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_GOTOFF_G0_NC,			301)	\
1453
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_GOTOFF_G1,			302)	\
1454
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_GOTOFF_G1_NC,			303)	\
1455
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_GOTOFF_G2,			304)	\
1456
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_GOTOFF_G2_NC,			305)	\
1457
_ELF_DEFINE_RELOC(R_AARCH64_MOVW_GOTOFF_G3,			306)	\
1458
_ELF_DEFINE_RELOC(R_AARCH64_GOTREL64,				307)	\
1459
_ELF_DEFINE_RELOC(R_AARCH64_GOTREL32,				308)	\
1460
_ELF_DEFINE_RELOC(R_AARCH64_GOT_LD_PREL19,			309)	\
1461
_ELF_DEFINE_RELOC(R_AARCH64_LD64_GOTOFF_LO15,			310)	\
1462
_ELF_DEFINE_RELOC(R_AARCH64_ADR_GOT_PAGE,			311)	\
1463
_ELF_DEFINE_RELOC(R_AARCH64_LD64_GOT_LO12_NC,			312)	\
1464
_ELF_DEFINE_RELOC(R_AARCH64_LD64_GOTPAGE_LO15,			313)	\
1465
_ELF_DEFINE_RELOC(R_AARCH64_TLSGD_ADR_PREL21,			512)	\
1466
_ELF_DEFINE_RELOC(R_AARCH64_TLSGD_ADR_PAGE21,			513)	\
1467
_ELF_DEFINE_RELOC(R_AARCH64_TLSGD_ADD_LO12_NC,			514)	\
1468
_ELF_DEFINE_RELOC(R_AARCH64_TLSGD_MOVW_G1,			515)	\
1469
_ELF_DEFINE_RELOC(R_AARCH64_TLSGD_MOVW_G0_NC,			516)	\
1470
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_ADR_PREL21,			517)	\
1471
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_ADR_PAGE21,			518)	\
1472
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_ADD_LO12_NC,			519)	\
1473
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_MOVW_G1,			520)	\
1474
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_MOVW_G0_NC,			521)	\
1475
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LD_PREL19,			522)	\
1476
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_MOVW_DTPREL_G2,		523)	\
1477
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_MOVW_DTPREL_G1,		524)	\
1478
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC,		525)	\
1479
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_MOVW_DTPREL_G0,		526)	\
1480
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC,		527)	\
1481
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_ADD_DTPREL_HI12,		529)	\
1482
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC,		530)	\
1483
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST8_DTPREL_LO12,		531)	\
1484
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC,		532)	\
1485
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST16_DTPREL_LO12,		533)	\
1486
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC,	534)	\
1487
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST32_DTPREL_LO12,		535)	\
1488
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC,	536)	\
1489
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST64_DTPREL_LO12,		537)	\
1490
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC,	538)	\
1491
_ELF_DEFINE_RELOC(R_AARCH64_TLSIE_MOVW_GOTTPREL_G1,		539)	\
1492
_ELF_DEFINE_RELOC(R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC,		540)	\
1493
_ELF_DEFINE_RELOC(R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21,		541)	\
1494
_ELF_DEFINE_RELOC(R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC,	542)	\
1495
_ELF_DEFINE_RELOC(R_AARCH64_TLSIE_LD_GOTTPREL_PREL19,		543)	\
1496
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_MOVW_TPREL_G2,		544)	\
1497
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_MOVW_TPREL_G1,		545)	\
1498
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_MOVW_TPREL_G1_NC,		546)	\
1499
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_MOVW_TPREL_G0,		547)	\
1500
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_MOVW_TPREL_G0_NC,		548)	\
1501
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_ADD_TPREL_HI12,		549)	\
1502
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_ADD_TPREL_LO12,		550)	\
1503
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_ADD_TPREL_LO12_NC,		551)	\
1504
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST8_TPREL_LO12,		552)	\
1505
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC,		553)	\
1506
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST16_TPREL_LO12,		554)	\
1507
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC,		555)	\
1508
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST32_TPREL_LO12,		556)	\
1509
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC,		557)	\
1510
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST64_TPREL_LO12,		558)	\
1511
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC,		559)	\
1512
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_LD_PREL19,			560)	\
1513
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_ADR_PREL21,			561)	\
1514
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_ADR_PAGE21,			562)	\
1515
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_LD64_LO12,			563)	\
1516
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_ADD_LO12,			564)	\
1517
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_OFF_G1,			565)	\
1518
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_OFF_G0_NC,			566)	\
1519
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_LDR,			567)	\
1520
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_ADD,			568)	\
1521
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC_CALL,			569)	\
1522
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST128_TPREL_LO12,		570)	\
1523
_ELF_DEFINE_RELOC(R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC,	571)	\
1524
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST128_DTPREL_LO12,		572)	\
1525
_ELF_DEFINE_RELOC(R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC,	573)	\
1526
_ELF_DEFINE_RELOC(R_AARCH64_COPY,				1024)	\
1527
_ELF_DEFINE_RELOC(R_AARCH64_GLOB_DAT,				1025)	\
1528
_ELF_DEFINE_RELOC(R_AARCH64_JUMP_SLOT,				1026)	\
1529
_ELF_DEFINE_RELOC(R_AARCH64_RELATIVE,				1027)	\
1530
_ELF_DEFINE_RELOC(R_AARCH64_TLS_DTPREL64,			1028)	\
1531
_ELF_DEFINE_RELOC(R_AARCH64_TLS_DTPMOD64,			1029)	\
1532
_ELF_DEFINE_RELOC(R_AARCH64_TLS_TPREL64,			1030)	\
1533
_ELF_DEFINE_RELOC(R_AARCH64_TLSDESC,				1031)	\
1534
_ELF_DEFINE_RELOC(R_AARCH64_IRELATIVE,				1032)
1413
1535
1414
/*
1536
/*
1415
 * These are the symbols used in the Sun ``Linkers and Loaders
1537
 * These are the symbols used in the Sun ``Linkers and Loaders
Lines 1633-1639 _ELF_DEFINE_RELOC(R_IA_64_LTV32MSB, 0x74) \ Link Here
1633
_ELF_DEFINE_RELOC(R_IA_64_LTV32LSB,		0x75)	\
1755
_ELF_DEFINE_RELOC(R_IA_64_LTV32LSB,		0x75)	\
1634
_ELF_DEFINE_RELOC(R_IA_64_LTV64MSB,		0x76)	\
1756
_ELF_DEFINE_RELOC(R_IA_64_LTV64MSB,		0x76)	\
1635
_ELF_DEFINE_RELOC(R_IA_64_LTV64LSB,		0x77)	\
1757
_ELF_DEFINE_RELOC(R_IA_64_LTV64LSB,		0x77)	\
1636
_ELF_DEFINE_RELOC(R_IA_64_PCREL21BIa,		0x79)	\
1758
_ELF_DEFINE_RELOC(R_IA_64_PCREL21BI,		0x79)	\
1637
_ELF_DEFINE_RELOC(R_IA_64_PCREL22,		0x7A)	\
1759
_ELF_DEFINE_RELOC(R_IA_64_PCREL22,		0x7A)	\
1638
_ELF_DEFINE_RELOC(R_IA_64_PCREL64I,		0x7B)	\
1760
_ELF_DEFINE_RELOC(R_IA_64_PCREL64I,		0x7B)	\
1639
_ELF_DEFINE_RELOC(R_IA_64_IPLTMSB,		0x80)	\
1761
_ELF_DEFINE_RELOC(R_IA_64_IPLTMSB,		0x80)	\
Lines 1723-1729 _ELF_DEFINE_RELOC(R_PPC_REL32, 26) \ Link Here
1723
_ELF_DEFINE_RELOC(R_PPC_PLT32,		27)	\
1845
_ELF_DEFINE_RELOC(R_PPC_PLT32,		27)	\
1724
_ELF_DEFINE_RELOC(R_PPC_PLTREL32,	28)	\
1846
_ELF_DEFINE_RELOC(R_PPC_PLTREL32,	28)	\
1725
_ELF_DEFINE_RELOC(R_PPC_PLT16_LO,	29)	\
1847
_ELF_DEFINE_RELOC(R_PPC_PLT16_LO,	29)	\
1726
_ELF_DEFINE_RELOC(R_PPL_PLT16_HI,	30)	\
1848
_ELF_DEFINE_RELOC(R_PPC_PLT16_HI,	30)	\
1727
_ELF_DEFINE_RELOC(R_PPC_PLT16_HA,	31)	\
1849
_ELF_DEFINE_RELOC(R_PPC_PLT16_HA,	31)	\
1728
_ELF_DEFINE_RELOC(R_PPC_SDAREL16,	32)	\
1850
_ELF_DEFINE_RELOC(R_PPC_SDAREL16,	32)	\
1729
_ELF_DEFINE_RELOC(R_PPC_SECTOFF,	33)	\
1851
_ELF_DEFINE_RELOC(R_PPC_SECTOFF,	33)	\
Lines 1926-1932 _ELF_DEFINE_RELOC(R_RISCV_SUB32, 39) \ Link Here
1926
_ELF_DEFINE_RELOC(R_RISCV_SUB64,		40)	\
2048
_ELF_DEFINE_RELOC(R_RISCV_SUB64,		40)	\
1927
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTINHERIT,	41)	\
2049
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTINHERIT,	41)	\
1928
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTENTRY,		42)	\
2050
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTENTRY,		42)	\
1929
_ELF_DEFINE_RELOC(R_RISCV_ALIGN			43)	\
2051
_ELF_DEFINE_RELOC(R_RISCV_ALIGN,		43)	\
1930
_ELF_DEFINE_RELOC(R_RISCV_RVC_BRANCH,		44)	\
2052
_ELF_DEFINE_RELOC(R_RISCV_RVC_BRANCH,		44)	\
1931
_ELF_DEFINE_RELOC(R_RISCV_RVC_JUMP,		45)
2053
_ELF_DEFINE_RELOC(R_RISCV_RVC_JUMP,		45)
1932
2054
Lines 2042-2047 _ELF_DEFINE_IA64_RELOCATIONS() \ Link Here
2042
_ELF_DEFINE_MIPS_RELOCATIONS()			\
2164
_ELF_DEFINE_MIPS_RELOCATIONS()			\
2043
_ELF_DEFINE_PPC32_RELOCATIONS()			\
2165
_ELF_DEFINE_PPC32_RELOCATIONS()			\
2044
_ELF_DEFINE_PPC64_RELOCATIONS()			\
2166
_ELF_DEFINE_PPC64_RELOCATIONS()			\
2167
_ELF_DEFINE_RISCV_RELOCATIONS()			\
2045
_ELF_DEFINE_SPARC_RELOCATIONS()			\
2168
_ELF_DEFINE_SPARC_RELOCATIONS()			\
2046
_ELF_DEFINE_X86_64_RELOCATIONS()
2169
_ELF_DEFINE_X86_64_RELOCATIONS()
2047
2170
(-)b/contrib/elftoolchain/common/native-elf-format (-1 / +3 lines)
Lines 1-6 Link Here
1
#!/bin/sh
1
#!/bin/sh
2
#
2
#
3
# $Id: native-elf-format 3186 2015-04-16 22:16:40Z emaste $
3
# $Id: native-elf-format 3293 2016-01-07 19:26:27Z emaste $
4
#
4
#
5
# Find the native ELF format for a host platform by compiling a
5
# Find the native ELF format for a host platform by compiling a
6
# test object and examining the resulting object.
6
# test object and examining the resulting object.
Lines 33-38 $1 ~ "Data:" { Link Here
33
$1 ~ "Machine:" {
33
$1 ~ "Machine:" {
34
        if (match($0, "Intel.*386")) {
34
        if (match($0, "Intel.*386")) {
35
            elfarch = "EM_386";
35
            elfarch = "EM_386";
36
        } else if (match($0, "MIPS")) {
37
            elfarch = "EM_MIPS";
36
        } else if (match($0, ".*[xX]86-64")) {
38
        } else if (match($0, ".*[xX]86-64")) {
37
            elfarch = "EM_X86_64";
39
            elfarch = "EM_X86_64";
38
        } else {
40
        } else {
(-)b/contrib/elftoolchain/cxxfilt/cxxfilt.c (-10 / +11 lines)
Lines 35-41 Link Here
35
35
36
#include "_elftc.h"
36
#include "_elftc.h"
37
37
38
ELFTC_VCSID("$Id: cxxfilt.c 3174 2015-03-27 17:13:41Z emaste $");
38
ELFTC_VCSID("$Id: cxxfilt.c 3356 2016-01-22 22:31:38Z jkoshy $");
39
39
40
#define	STRBUFSZ	8192
40
#define	STRBUFSZ	8192
41
41
Lines 112-122 find_format(const char *fstr) Link Here
112
}
112
}
113
113
114
static char *
114
static char *
115
demangle(char *name, int strict, int *pos)
115
demangle(char *name, int strict, size_t *pos)
116
{
116
{
117
	static char dem[STRBUFSZ];
117
	static char dem[STRBUFSZ];
118
	char nb[STRBUFSZ];
118
	char nb[STRBUFSZ];
119
	int p, t;
119
	size_t p, t;
120
120
121
	if (stripus && *name == '_') {
121
	if (stripus && *name == '_') {
122
		strncpy(nb, name + 1, sizeof(nb) - 1);
122
		strncpy(nb, name + 1, sizeof(nb) - 1);
Lines 128-137 demangle(char *name, int strict, int *pos) Link Here
128
	nb[sizeof(nb) - 1] = '\0';
128
	nb[sizeof(nb) - 1] = '\0';
129
129
130
	p = strlen(nb);
130
	p = strlen(nb);
131
	if (p <= 0)
131
	if (p == 0)
132
		return NULL;
132
		return NULL;
133
133
134
	while (elftc_demangle(nb, dem, sizeof(dem), format) < 0) {
134
	while (elftc_demangle(nb, dem, sizeof(dem), (unsigned) format) < 0) {
135
		if (!strict && p > 1) {
135
		if (!strict && p > 1) {
136
			nb[--p] = '\0';
136
			nb[--p] = '\0';
137
			continue;
137
			continue;
Lines 149-155 int Link Here
149
main(int argc, char **argv)
149
main(int argc, char **argv)
150
{
150
{
151
	char *dem, buf[STRBUFSZ];
151
	char *dem, buf[STRBUFSZ];
152
	int c, i, p, s, opt;
152
	size_t i, p, s;
153
	int c, n, opt;
153
154
154
	while ((opt = getopt_long(argc, argv, "_nps:V", longopts, NULL)) !=
155
	while ((opt = getopt_long(argc, argv, "_nps:V", longopts, NULL)) !=
155
	    -1) {
156
	    -1) {
Lines 182-190 main(int argc, char **argv) Link Here
182
	argc -= optind;
183
	argc -= optind;
183
184
184
	if (*argv != NULL) {
185
	if (*argv != NULL) {
185
		for (i = 0; i < argc; i++) {
186
		for (n = 0; n < argc; n++) {
186
			if ((dem = demangle(argv[i], 1, NULL)) == NULL)
187
			if ((dem = demangle(argv[n], 1, NULL)) == NULL)
187
				fprintf(stderr, "Failed: %s\n", argv[i]);
188
				fprintf(stderr, "Failed: %s\n", argv[n]);
188
			else
189
			else
189
				printf("%s\n", dem);
190
				printf("%s\n", dem);
190
		}
191
		}
Lines 213-219 main(int argc, char **argv) Link Here
213
				if ((size_t) p >= sizeof(buf) - 1)
214
				if ((size_t) p >= sizeof(buf) - 1)
214
					warnx("buffer overflowed");
215
					warnx("buffer overflowed");
215
				else
216
				else
216
					buf[p++] = c;
217
					buf[p++] = (char) c;
217
			}
218
			}
218
219
219
		}
220
		}
(-)b/contrib/elftoolchain/elfcopy/Makefile (-5 / +18 lines)
Lines 1-10 Link Here
1
# $Id: Makefile 2290 2011-12-04 07:20:46Z jkoshy $
1
# $Id: Makefile 3381 2016-01-30 19:39:47Z jkoshy $
2
2
3
TOP=	..
3
TOP=	..
4
4
5
.include "${TOP}/mk/elftoolchain.components.mk"
6
5
PROG=	elfcopy
7
PROG=	elfcopy
6
8
7
SRCS=	archive.c ascii.c binary.c main.c sections.c segments.c symbols.c
9
SRCS=	archive.c ascii.c binary.c main.c sections.c segments.c \
10
	symbols.c
8
11
9
WARNS?=	5
12
WARNS?=	5
10
13
Lines 15-28 LDADD= -lelf -lelftc Link Here
15
LDADD+= -larchive
18
LDADD+= -larchive
16
.endif
19
.endif
17
20
21
.if defined(WITH_PE) && ${WITH_PE:tl} == "yes"
22
SRCS+=	pe.c
23
CFLAGS+= -DWITH_PE=1
24
25
DPADD+=	${LIBPE}
26
LDADD+=	-lpe
27
.endif
28
18
MAN=	elfcopy.1 mcs.1 strip.1
29
MAN=	elfcopy.1 mcs.1 strip.1
30
MLINKS=	elfcopy.1 objcopy.1
19
31
20
NO_SHARED?=	yes
32
NO_SHARED?=	yes
21
33
22
LINKS=	${BINDIR}/elfcopy ${BINDIR}/strip 	\
34
LINKS=	${BINDIR}/elfcopy ${BINDIR}/mcs		\
23
	${BINDIR}/elfcopy ${BINDIR}/mcs
35
	${BINDIR}/elfcopy ${BINDIR}/objcopy 	\
36
	${BINDIR}/elfcopy ${BINDIR}/strip
24
37
25
EXTRA_TARGETS=	strip mcs
38
EXTRA_TARGETS=	mcs strip objcopy
26
39
27
CLEANFILES+=	${EXTRA_TARGETS}
40
CLEANFILES+=	${EXTRA_TARGETS}
28
41
(-)b/contrib/elftoolchain/elfcopy/archive.c (-3 / +3 lines)
Lines 38-44 Link Here
38
38
39
#include "elfcopy.h"
39
#include "elfcopy.h"
40
40
41
ELFTC_VCSID("$Id: archive.c 3174 2015-03-27 17:13:41Z emaste $");
41
ELFTC_VCSID("$Id: archive.c 3287 2015-12-31 16:58:48Z emaste $");
42
42
43
#define _ARMAG_LEN 8		/* length of ar magic string */
43
#define _ARMAG_LEN 8		/* length of ar magic string */
44
#define _ARHDR_LEN 60		/* length of ar header */
44
#define _ARHDR_LEN 60		/* length of ar header */
Lines 382-388 ac_read_objs(struct elfcopy *ecp, int ifd) Link Here
382
	if (lseek(ifd, 0, SEEK_SET) == -1)
382
	if (lseek(ifd, 0, SEEK_SET) == -1)
383
		err(EXIT_FAILURE, "lseek failed");
383
		err(EXIT_FAILURE, "lseek failed");
384
	if ((a = archive_read_new()) == NULL)
384
	if ((a = archive_read_new()) == NULL)
385
		errx(EXIT_FAILURE, "%s", archive_error_string(a));
385
		errx(EXIT_FAILURE, "archive_read_new failed");
386
	archive_read_support_format_ar(a);
386
	archive_read_support_format_ar(a);
387
	AC(archive_read_open_fd(a, ifd, 10240));
387
	AC(archive_read_open_fd(a, ifd, 10240));
388
	for(;;) {
388
	for(;;) {
Lines 443-449 ac_write_objs(struct elfcopy *ecp, int ofd) Link Here
443
	int			 nr;
443
	int			 nr;
444
444
445
	if ((a = archive_write_new()) == NULL)
445
	if ((a = archive_write_new()) == NULL)
446
		errx(EXIT_FAILURE, "%s", archive_error_string(a));
446
		errx(EXIT_FAILURE, "archive_write_new failed");
447
	archive_write_set_format_ar_svr4(a);
447
	archive_write_set_format_ar_svr4(a);
448
	AC(archive_write_open_fd(a, ofd));
448
	AC(archive_write_open_fd(a, ofd));
449
449
(-)b/contrib/elftoolchain/elfcopy/elfcopy.1 (-4 / +15 lines)
Lines 21-33 Link Here
21
.\" out of the use of this software, even if advised of the possibility of
21
.\" out of the use of this software, even if advised of the possibility of
22
.\" such damage.
22
.\" such damage.
23
.\"
23
.\"
24
.\" $Id: elfcopy.1 3266 2015-12-07 15:38:26Z emaste $
24
.\" $Id: elfcopy.1 3381 2016-01-30 19:39:47Z jkoshy $
25
.\"
25
.\"
26
.Dd December 7, 2015
26
.Dd January 29, 2016
27
.Os
27
.Os
28
.Dt ELFCOPY 1
28
.Dt ELFCOPY 1
29
.Sh NAME
29
.Sh NAME
30
.Nm elfcopy
30
.Nm elfcopy ,
31
.Nm objcopy
31
.Nd copy and translate object files
32
.Nd copy and translate object files
32
.Sh SYNOPSIS
33
.Sh SYNOPSIS
33
.Nm
34
.Nm
Lines 85-91 Link Here
85
.Sh DESCRIPTION
86
.Sh DESCRIPTION
86
The
87
The
87
.Nm
88
.Nm
88
utility copies the content of the ELF object named by argument
89
utility copies the content of the binary object named by argument
89
.Ar infile
90
.Ar infile
90
to that named by argument
91
to that named by argument
91
.Ar outfile ,
92
.Ar outfile ,
Lines 121-126 to the output. Link Here
121
.It Fl O Ar objformat | Fl -output-target= Ns Ar objformat
122
.It Fl O Ar objformat | Fl -output-target= Ns Ar objformat
122
Write the output file using the object format specified in argument
123
Write the output file using the object format specified in argument
123
.Ar objformat .
124
.Ar objformat .
125
The argument
126
.Ar objformat
127
should be one of the target names recognized by
128
.Xr elftc_bfd_find_target 3 .
124
.It Fl R Ar sectionname | Fl -remove-section= Ns Ar sectionname
129
.It Fl R Ar sectionname | Fl -remove-section= Ns Ar sectionname
125
Remove any section with name
130
Remove any section with name
126
.Ar sectionname
131
.Ar sectionname
Lines 330-337 Do not copy symbols that are not needed for relocation processing. Link Here
330
.Xr mcs 1 ,
335
.Xr mcs 1 ,
331
.Xr strip 1 ,
336
.Xr strip 1 ,
332
.Xr elf 3 ,
337
.Xr elf 3 ,
338
.Xr elftc_bfd_find_target 3 ,
333
.Xr ar 5 ,
339
.Xr ar 5 ,
334
.Xr elf 5
340
.Xr elf 5
341
.Sh COMPATIBILITY
342
The
343
.Nm
344
utility is expected to be option compatible with GNU
345
.Nm objcopy .
335
.Sh HISTORY
346
.Sh HISTORY
336
.Nm
347
.Nm
337
has been implemented by
348
has been implemented by
(-)b/contrib/elftoolchain/elfcopy/elfcopy.h (-1 / +2 lines)
Lines 23-29 Link Here
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
24
 * SUCH DAMAGE.
25
 *
25
 *
26
 * $Id: elfcopy.h 3221 2015-05-24 23:42:43Z kaiwang27 $
26
 * $Id: elfcopy.h 3310 2016-01-10 09:10:54Z kaiwang27 $
27
 */
27
 */
28
28
29
#include <sys/queue.h>
29
#include <sys/queue.h>
Lines 287-292 struct section *create_external_section(struct elfcopy *_ecp, const char *_name, Link Here
287
    int _loadable);
287
    int _loadable);
288
void	create_external_symtab(struct elfcopy *_ecp);
288
void	create_external_symtab(struct elfcopy *_ecp);
289
void	create_ihex(int _ifd, int _ofd);
289
void	create_ihex(int _ifd, int _ofd);
290
void	create_pe(struct elfcopy *_ecp, int _ifd, int _ofd);
290
void	create_scn(struct elfcopy *_ecp);
291
void	create_scn(struct elfcopy *_ecp);
291
void	create_srec(struct elfcopy *_ecp, int _ifd, int _ofd, const char *_ofn);
292
void	create_srec(struct elfcopy *_ecp, int _ifd, int _ofd, const char *_ofn);
292
void	create_symtab(struct elfcopy *_ecp);
293
void	create_symtab(struct elfcopy *_ecp);
(-)b/contrib/elftoolchain/elfcopy/main.c (-2 / +16 lines)
Lines 39-45 Link Here
39
39
40
#include "elfcopy.h"
40
#include "elfcopy.h"
41
41
42
ELFTC_VCSID("$Id: main.c 3268 2015-12-07 20:30:55Z emaste $");
42
ELFTC_VCSID("$Id: main.c 3381 2016-01-30 19:39:47Z jkoshy $");
43
43
44
enum options
44
enum options
45
{
45
{
Lines 722-727 create_file(struct elfcopy *ecp, const char *src, const char *dst) Link Here
722
				create_srec(ecp, ofd, ofd0,
722
				create_srec(ecp, ofd, ofd0,
723
				    dst != NULL ? dst : src);
723
				    dst != NULL ? dst : src);
724
				break;
724
				break;
725
			case ETF_PE:
726
			case ETF_EFI:
727
#if	WITH_PE
728
				create_pe(ecp, ofd, ofd0);
729
#else
730
				errx(EXIT_FAILURE, "PE/EFI support not enabled"
731
				    " at compile time");
732
#endif
733
				break;
725
			default:
734
			default:
726
				errx(EXIT_FAILURE, "Internal: unsupported"
735
				errx(EXIT_FAILURE, "Internal: unsupported"
727
				    " output flavour %d", ecp->oec);
736
				    " output flavour %d", ecp->oec);
Lines 1345-1350 set_output_target(struct elfcopy *ecp, const char *target_name) Link Here
1345
		ecp->oed = elftc_bfd_target_byteorder(tgt);
1354
		ecp->oed = elftc_bfd_target_byteorder(tgt);
1346
		ecp->oem = elftc_bfd_target_machine(tgt);
1355
		ecp->oem = elftc_bfd_target_machine(tgt);
1347
	}
1356
	}
1357
	if (ecp->otf == ETF_EFI || ecp->otf == ETF_PE)
1358
		ecp->oem = elftc_bfd_target_machine(tgt);
1359
1348
	ecp->otgt = target_name;
1360
	ecp->otgt = target_name;
1349
}
1361
}
1350
1362
Lines 1366-1372 set_osabi(struct elfcopy *ecp, const char *abi) Link Here
1366
1378
1367
#define	ELFCOPY_USAGE_MESSAGE	"\
1379
#define	ELFCOPY_USAGE_MESSAGE	"\
1368
Usage: %s [options] infile [outfile]\n\
1380
Usage: %s [options] infile [outfile]\n\
1369
  Transform an ELF object.\n\n\
1381
  Transform object files.\n\n\
1370
  Options:\n\
1382
  Options:\n\
1371
  -d | -g | --strip-debug      Remove debugging information from the output.\n\
1383
  -d | -g | --strip-debug      Remove debugging information from the output.\n\
1372
  -j SECTION | --only-section=SECTION\n\
1384
  -j SECTION | --only-section=SECTION\n\
Lines 1382-1387 Usage: %s [options] infile [outfile]\n\ Link Here
1382
  -N SYM | --strip-symbol=SYM  Do not copy symbol SYM to the output.\n\
1394
  -N SYM | --strip-symbol=SYM  Do not copy symbol SYM to the output.\n\
1383
  -O FORMAT | --output-target=FORMAT\n\
1395
  -O FORMAT | --output-target=FORMAT\n\
1384
                               Specify object format for the output file.\n\
1396
                               Specify object format for the output file.\n\
1397
                               FORMAT should be a target name understood by\n\
1398
                               elftc_bfd_find_target(3).\n\
1385
  -R NAME | --remove-section=NAME\n\
1399
  -R NAME | --remove-section=NAME\n\
1386
                               Remove the named section.\n\
1400
                               Remove the named section.\n\
1387
  -S | --strip-all             Remove all symbol and relocation information\n\
1401
  -S | --strip-all             Remove all symbol and relocation information\n\
(-)b/contrib/elftoolchain/elfcopy/pe.c (+233 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/param.h>
28
#include <err.h>
29
#include <gelf.h>
30
#include <libpe.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <time.h>
34
35
#include "elfcopy.h"
36
37
ELFTC_VCSID("$Id: pe.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
38
39
/* Convert ELF object to Portable Executable (PE). */
40
void
41
create_pe(struct elfcopy *ecp, int ifd, int ofd)
42
{
43
	Elf *e;
44
	Elf_Scn *scn;
45
	Elf_Data *d;
46
	GElf_Ehdr eh;
47
	GElf_Shdr sh;
48
	PE *pe;
49
	PE_Scn *ps;
50
	PE_SecHdr psh;
51
	PE_CoffHdr pch;
52
	PE_OptHdr poh;
53
	PE_Object po;
54
	PE_Buffer *pb;
55
	const char *name;
56
	size_t indx;
57
	int elferr, i;
58
59
	if (ecp->otf == ETF_EFI || ecp->oem == EM_X86_64)
60
		po = PE_O_PE32P;
61
	else
62
		po = PE_O_PE32;
63
64
	if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
65
		errx(EXIT_FAILURE, "elf_begin() failed: %s",
66
		    elf_errmsg(-1));
67
68
	if (gelf_getehdr(e, &eh) == NULL)
69
		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
70
		    elf_errmsg(-1));
71
72
	if (elf_getshstrndx(ecp->ein, &indx) == 0)
73
		errx(EXIT_FAILURE, "elf_getshstrndx() failed: %s",
74
		    elf_errmsg(-1));
75
76
	if ((pe = pe_init(ofd, PE_C_WRITE, po)) == NULL)
77
		err(EXIT_FAILURE, "pe_init() failed");
78
79
	/* Setup PE COFF header. */
80
	memset(&pch, 0, sizeof(pch));
81
	switch (ecp->oem) {
82
	case EM_386:
83
		pch.ch_machine = IMAGE_FILE_MACHINE_I386;
84
		break;
85
	case EM_X86_64:
86
		pch.ch_machine = IMAGE_FILE_MACHINE_AMD64;
87
		break;
88
	default:
89
		pch.ch_machine = IMAGE_FILE_MACHINE_UNKNOWN;
90
		break;
91
	}
92
	pch.ch_timestamp = (uint32_t) time(NULL);
93
	if (pe_update_coff_header(pe, &pch) < 0)
94
		err(EXIT_FAILURE, "pe_update_coff_header() failed");
95
96
	/* Setup PE optional header. */
97
	memset(&poh, 0, sizeof(poh));
98
	if (ecp->otf == ETF_EFI)
99
		poh.oh_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
100
	poh.oh_entry = (uint32_t) eh.e_entry;
101
102
	/*
103
	 * Default section alignment and file alignment. (Here the
104
	 * section alignment is set to the default page size of the
105
	 * archs supported. We should use different section alignment
106
	 * for some arch. (e.g. IA64)
107
	 */
108
	poh.oh_secalign = 0x1000;
109
	poh.oh_filealign = 0x200;
110
111
	/* Copy sections. */
112
	scn = NULL;
113
	while ((scn = elf_nextscn(e, scn)) != NULL) {
114
115
		/*
116
		 * Read in ELF section.
117
		 */
118
119
		if (gelf_getshdr(scn, &sh) == NULL) {
120
			warnx("gelf_getshdr() failed: %s", elf_errmsg(-1));
121
			(void) elf_errno();
122
			continue;
123
		}
124
		if ((name = elf_strptr(ecp->ein, indx, sh.sh_name)) ==
125
		    NULL) {
126
			warnx("elf_strptr() failed: %s", elf_errmsg(-1));
127
			(void) elf_errno();
128
			continue;
129
		}
130
131
		/* Skip sections unneeded. */
132
		if (strcmp(name, ".shstrtab") == 0 ||
133
		    strcmp(name, ".symtab") == 0 ||
134
		    strcmp(name, ".strtab") == 0)
135
			continue;
136
137
		if ((d = elf_getdata(scn, NULL)) == NULL) {
138
			warnx("elf_getdata() failed: %s", elf_errmsg(-1));
139
			(void) elf_errno();
140
			continue;
141
		}
142
143
		if (strcmp(name, ".text") == 0) {
144
			poh.oh_textbase = (uint32_t) sh.sh_addr;
145
			poh.oh_textsize = (uint32_t) roundup(sh.sh_size,
146
			    poh.oh_filealign);
147
		} else {
148
			if (po == PE_O_PE32 && strcmp(name, ".data") == 0)
149
				poh.oh_database = sh.sh_addr;
150
			if (sh.sh_type == SHT_NOBITS)
151
				poh.oh_bsssize += (uint32_t)
152
				    roundup(sh.sh_size, poh.oh_filealign);
153
			else if (sh.sh_flags & SHF_ALLOC)
154
				poh.oh_datasize += (uint32_t)
155
				    roundup(sh.sh_size, poh.oh_filealign);
156
		}
157
158
		/*
159
		 * Create PE/COFF section.
160
		 */
161
162
		if ((ps = pe_newscn(pe)) == NULL) {
163
			warn("pe_newscn() failed");
164
			continue;
165
		}
166
167
		/*
168
		 * Setup PE/COFF section header. The section name is not
169
		 * NUL-terminated if its length happens to be 8. Long
170
		 * section name should be truncated for PE image according
171
		 * to the PE/COFF specification.
172
		 */
173
		memset(&psh, 0, sizeof(psh));
174
		strncpy(psh.sh_name, name, sizeof(psh.sh_name));
175
		psh.sh_addr = sh.sh_addr;
176
		psh.sh_virtsize = sh.sh_size;
177
		if (sh.sh_type != SHT_NOBITS)
178
			psh.sh_rawsize = sh.sh_size;
179
		else
180
			psh.sh_char |= IMAGE_SCN_CNT_UNINITIALIZED_DATA;
181
182
		/*
183
		 * Translate ELF section flags to PE/COFF section flags.
184
		 */
185
		psh.sh_char |= IMAGE_SCN_MEM_READ;
186
		if (sh.sh_flags & SHF_WRITE)
187
			psh.sh_char |= IMAGE_SCN_MEM_WRITE;
188
		if (sh.sh_flags & SHF_EXECINSTR)
189
			psh.sh_char |= IMAGE_SCN_MEM_EXECUTE |
190
			    IMAGE_SCN_CNT_CODE;
191
		if ((sh.sh_flags & SHF_ALLOC) && (psh.sh_char & 0xF0) == 0)
192
			psh.sh_char |= IMAGE_SCN_CNT_INITIALIZED_DATA;
193
		for (i = 0xE; i > 0; i--) {
194
			if (sh.sh_addralign & (1U << (i - 1))) {
195
				psh.sh_char |= i << 20;
196
				break;
197
			}
198
		}
199
200
		/* Mark relocation section "discardable". */
201
		if (strcmp(name, ".reloc") == 0)
202
			psh.sh_char |= IMAGE_SCN_MEM_DISCARDABLE;
203
204
		if (pe_update_section_header(ps, &psh) < 0) {
205
			warn("pe_update_section_header() failed");
206
			continue;
207
		}
208
209
		/* Copy section content. */
210
		if ((pb = pe_newbuffer(ps)) == NULL) {
211
			warn("pe_newbuffer() failed");
212
			continue;
213
		}
214
		pb->pb_align = 1;
215
		pb->pb_off = 0;
216
		pb->pb_size = sh.sh_size;
217
		pb->pb_buf = d->d_buf;
218
	}
219
	elferr = elf_errno();
220
	if (elferr != 0)
221
		warnx("elf_nextscn() failed: %s", elf_errmsg(elferr));
222
223
	/* Update PE optional header. */
224
	if (pe_update_opt_header(pe, &poh) < 0)
225
		err(EXIT_FAILURE, "pe_update_opt_header() failed");
226
227
	/* Write out PE/COFF object. */
228
	if (pe_update(pe) < 0)
229
		err(EXIT_FAILURE, "pe_update() failed");
230
231
	pe_finish(pe);
232
	elf_end(e);
233
}
(-)b/contrib/elftoolchain/elfcopy/sections.c (-31 / +76 lines)
Lines 34-40 Link Here
34
34
35
#include "elfcopy.h"
35
#include "elfcopy.h"
36
36
37
ELFTC_VCSID("$Id: sections.c 3272 2015-12-11 20:00:54Z kaiwang27 $");
37
ELFTC_VCSID("$Id: sections.c 3346 2016-01-17 20:09:15Z kaiwang27 $");
38
38
39
static void	add_gnu_debuglink(struct elfcopy *ecp);
39
static void	add_gnu_debuglink(struct elfcopy *ecp);
40
static uint32_t calc_crc32(const char *p, size_t len, uint32_t crc);
40
static uint32_t calc_crc32(const char *p, size_t len, uint32_t crc);
Lines 223-228 static int Link Here
223
is_debug_section(const char *name)
223
is_debug_section(const char *name)
224
{
224
{
225
	const char *dbg_sec[] = {
225
	const char *dbg_sec[] = {
226
		".apple_",
226
		".debug",
227
		".debug",
227
		".gnu.linkonce.wi.",
228
		".gnu.linkonce.wi.",
228
		".line",
229
		".line",
Lines 369-375 create_scn(struct elfcopy *ecp) Link Here
369
	is = NULL;
370
	is = NULL;
370
	while ((is = elf_nextscn(ecp->ein, is)) != NULL) {
371
	while ((is = elf_nextscn(ecp->ein, is)) != NULL) {
371
		if (gelf_getshdr(is, &ish) == NULL)
372
		if (gelf_getshdr(is, &ish) == NULL)
372
			errx(EXIT_FAILURE, "219 gelf_getshdr failed: %s",
373
			errx(EXIT_FAILURE, "gelf_getshdr failed: %s",
373
			    elf_errmsg(-1));
374
			    elf_errmsg(-1));
374
		if ((name = elf_strptr(ecp->ein, indx, ish.sh_name)) == NULL)
375
		if ((name = elf_strptr(ecp->ein, indx, ish.sh_name)) == NULL)
375
			errx(EXIT_FAILURE, "elf_strptr failed: %s",
376
			errx(EXIT_FAILURE, "elf_strptr failed: %s",
Lines 416-427 create_scn(struct elfcopy *ecp) Link Here
416
			 * is loadable, but if user explicitly set section flags
417
			 * is loadable, but if user explicitly set section flags
417
			 * while neither "load" nor "alloc" is set, we make the
418
			 * while neither "load" nor "alloc" is set, we make the
418
			 * section unloadable.
419
			 * section unloadable.
420
			 *
421
			 * Sections in relocatable object is loadable if
422
			 * section flag SHF_ALLOC is set.
419
			 */
423
			 */
420
			if (sec_flags &&
424
			if (sec_flags &&
421
			    (sec_flags & (SF_LOAD | SF_ALLOC)) == 0)
425
			    (sec_flags & (SF_LOAD | SF_ALLOC)) == 0)
422
				s->loadable = 0;
426
				s->loadable = 0;
423
			else
427
			else {
424
				s->loadable = add_to_inseg_list(ecp, s);
428
				s->loadable = add_to_inseg_list(ecp, s);
429
				if ((ecp->flags & RELOCATABLE) &&
430
				    (ish.sh_flags & SHF_ALLOC))
431
					s->loadable = 1;
432
			}
425
		} else {
433
		} else {
426
			/* Assuming .shstrtab is "unloadable". */
434
			/* Assuming .shstrtab is "unloadable". */
427
			s		= ecp->shstrtab;
435
			s		= ecp->shstrtab;
Lines 875-884 resync_sections(struct elfcopy *ecp) Link Here
875
		if (s->align == 0)
883
		if (s->align == 0)
876
			s->align = 1;
884
			s->align = 1;
877
		if (off <= s->off) {
885
		if (off <= s->off) {
878
			if (!s->loadable)
886
			if (!s->loadable || (ecp->flags & RELOCATABLE))
879
				s->off = roundup(off, s->align);
887
				s->off = roundup(off, s->align);
880
		} else {
888
		} else {
881
			if (s->loadable)
889
			if (s->loadable && (ecp->flags & RELOCATABLE) == 0)
882
				warnx("moving loadable section %s, "
890
				warnx("moving loadable section %s, "
883
				    "is this intentional?", s->name);
891
				    "is this intentional?", s->name);
884
			s->off = roundup(off, s->align);
892
			s->off = roundup(off, s->align);
Lines 1028-1035 print_section(struct section *s) Link Here
1028
		print_data(s->buf, s->sz);
1036
		print_data(s->buf, s->sz);
1029
	} else {
1037
	} else {
1030
		id = NULL;
1038
		id = NULL;
1031
		while ((id = elf_getdata(s->is, id)) != NULL)
1039
		while ((id = elf_getdata(s->is, id)) != NULL ||
1040
		    (id = elf_rawdata(s->is, id)) != NULL) {
1041
			(void) elf_errno();
1032
			print_data(id->d_buf, id->d_size);
1042
			print_data(id->d_buf, id->d_size);
1043
		}
1033
		elferr = elf_errno();
1044
		elferr = elf_errno();
1034
		if (elferr != 0)
1045
		if (elferr != 0)
1035
			errx(EXIT_FAILURE, "elf_getdata() failed: %s",
1046
			errx(EXIT_FAILURE, "elf_getdata() failed: %s",
Lines 1049-1055 read_section(struct section *s, size_t *size) Link Here
1049
	sz = 0;
1060
	sz = 0;
1050
	b = NULL;
1061
	b = NULL;
1051
	id = NULL;
1062
	id = NULL;
1052
	while ((id = elf_getdata(s->is, id)) != NULL) {
1063
	while ((id = elf_getdata(s->is, id)) != NULL ||
1064
	    (id = elf_rawdata(s->is, id)) != NULL) {
1065
		(void) elf_errno();
1053
		if (b == NULL)
1066
		if (b == NULL)
1054
			b = malloc(id->d_size);
1067
			b = malloc(id->d_size);
1055
		else
1068
		else
Lines 1077-1086 copy_shdr(struct elfcopy *ecp, struct section *s, const char *name, int copy, Link Here
1077
	GElf_Shdr ish, osh;
1090
	GElf_Shdr ish, osh;
1078
1091
1079
	if (gelf_getshdr(s->is, &ish) == NULL)
1092
	if (gelf_getshdr(s->is, &ish) == NULL)
1080
		errx(EXIT_FAILURE, "526 gelf_getshdr() failed: %s",
1093
		errx(EXIT_FAILURE, "gelf_getshdr() failed: %s",
1081
		    elf_errmsg(-1));
1094
		    elf_errmsg(-1));
1082
	if (gelf_getshdr(s->os, &osh) == NULL)
1095
	if (gelf_getshdr(s->os, &osh) == NULL)
1083
		errx(EXIT_FAILURE, "529 gelf_getshdr() failed: %s",
1096
		errx(EXIT_FAILURE, "gelf_getshdr() failed: %s",
1084
		    elf_errmsg(-1));
1097
		    elf_errmsg(-1));
1085
1098
1086
	if (copy)
1099
	if (copy)
Lines 1097-1115 copy_shdr(struct elfcopy *ecp, struct section *s, const char *name, int copy, Link Here
1097
1110
1098
		if (sec_flags) {
1111
		if (sec_flags) {
1099
			osh.sh_flags = 0;
1112
			osh.sh_flags = 0;
1100
			if (sec_flags & SF_ALLOC) {
1113
			if (sec_flags & SF_ALLOC)
1101
				osh.sh_flags |= SHF_ALLOC;
1114
				osh.sh_flags |= SHF_ALLOC;
1102
				if (!s->loadable)
1103
					warnx("set SHF_ALLOC flag for "
1104
					    "unloadable section %s",
1105
					    s->name);
1106
			}
1107
			if ((sec_flags & SF_READONLY) == 0)
1115
			if ((sec_flags & SF_READONLY) == 0)
1108
				osh.sh_flags |= SHF_WRITE;
1116
				osh.sh_flags |= SHF_WRITE;
1109
			if (sec_flags & SF_CODE)
1117
			if (sec_flags & SF_CODE)
1110
				osh.sh_flags |= SHF_EXECINSTR;
1118
				osh.sh_flags |= SHF_EXECINSTR;
1119
			if ((sec_flags & SF_CONTENTS) &&
1120
			    s->type == SHT_NOBITS && s->sz > 0) {
1121
				/*
1122
				 * Convert SHT_NOBITS section to section with
1123
				 * (zero'ed) content on file.
1124
				 */
1125
				osh.sh_type = s->type = SHT_PROGBITS;
1126
				if ((s->buf = calloc(1, s->sz)) == NULL)
1127
					err(EXIT_FAILURE, "malloc failed");
1128
				s->nocopy = 1;
1129
			}
1111
		} else {
1130
		} else {
1112
			osh.sh_flags = ish.sh_flags;
1131
			osh.sh_flags = ish.sh_flags;
1132
			/*
1133
			 * Newer binutils as(1) emits the section flag
1134
			 * SHF_INFO_LINK for relocation sections. elfcopy
1135
			 * emits this flag in the output section if it's
1136
			 * missing in the input section, to remain compatible
1137
			 * with binutils.
1138
			 */
1113
			if (ish.sh_type == SHT_REL || ish.sh_type == SHT_RELA)
1139
			if (ish.sh_type == SHT_REL || ish.sh_type == SHT_RELA)
1114
				osh.sh_flags |= SHF_INFO_LINK;
1140
				osh.sh_flags |= SHF_INFO_LINK;
1115
		}
1141
		}
Lines 1135-1145 copy_data(struct section *s) Link Here
1135
		return;
1161
		return;
1136
1162
1137
	if ((id = elf_getdata(s->is, NULL)) == NULL) {
1163
	if ((id = elf_getdata(s->is, NULL)) == NULL) {
1138
		elferr = elf_errno();
1164
		(void) elf_errno();
1139
		if (elferr != 0)
1165
		if ((id = elf_rawdata(s->is, NULL)) == NULL) {
1140
			errx(EXIT_FAILURE, "elf_getdata() failed: %s",
1166
			elferr = elf_errno();
1141
			    elf_errmsg(elferr));
1167
			if (elferr != 0)
1142
		return;
1168
				errx(EXIT_FAILURE, "failed to read section:"
1169
				    " %s", s->name);
1170
			return;
1171
		}
1143
	}
1172
	}
1144
1173
1145
	if ((od = elf_newdata(s->os)) == NULL)
1174
	if ((od = elf_newdata(s->os)) == NULL)
Lines 1245-1250 insert_sections(struct elfcopy *ecp) Link Here
1245
	struct sec_add	*sa;
1274
	struct sec_add	*sa;
1246
	struct section	*s;
1275
	struct section	*s;
1247
	size_t		 off;
1276
	size_t		 off;
1277
	uint64_t	 stype;
1248
1278
1249
	/* Put these sections in the end of current list. */
1279
	/* Put these sections in the end of current list. */
1250
	off = 0;
1280
	off = 0;
Lines 1259-1266 insert_sections(struct elfcopy *ecp) Link Here
1259
1289
1260
		/* TODO: Add section header vma/lma, flag changes here */
1290
		/* TODO: Add section header vma/lma, flag changes here */
1261
1291
1292
		/*
1293
		 * The default section type for user added section is
1294
		 * SHT_PROGBITS. If the section name match certain patterns,
1295
		 * elfcopy will try to set a more appropriate section type.
1296
		 * However, data type is always set to ELF_T_BYTE and no
1297
		 * translation is performed by libelf.
1298
		 */
1299
		stype = SHT_PROGBITS;
1300
		if (strcmp(sa->name, ".note") == 0 ||
1301
		    strncmp(sa->name, ".note.", strlen(".note.")) == 0)
1302
			stype = SHT_NOTE;
1303
1262
		(void) create_external_section(ecp, sa->name, NULL, sa->content,
1304
		(void) create_external_section(ecp, sa->name, NULL, sa->content,
1263
		    sa->size, off, SHT_PROGBITS, ELF_T_BYTE, 0, 1, 0, 0);
1305
		    sa->size, off, stype, ELF_T_BYTE, 0, 1, 0, 0);
1264
	}
1306
	}
1265
}
1307
}
1266
1308
Lines 1285-1291 update_shdr(struct elfcopy *ecp, int update_link) Link Here
1285
			continue;
1327
			continue;
1286
1328
1287
		if (gelf_getshdr(s->os, &osh) == NULL)
1329
		if (gelf_getshdr(s->os, &osh) == NULL)
1288
			errx(EXIT_FAILURE, "668 gelf_getshdr failed: %s",
1330
			errx(EXIT_FAILURE, "gelf_getshdr failed: %s",
1289
			    elf_errmsg(-1));
1331
			    elf_errmsg(-1));
1290
1332
1291
		/* Find section name in string table and set sh_name. */
1333
		/* Find section name in string table and set sh_name. */
Lines 1364-1370 set_shstrtab(struct elfcopy *ecp) Link Here
1364
	}
1406
	}
1365
1407
1366
	if (gelf_getshdr(s->os, &sh) == NULL)
1408
	if (gelf_getshdr(s->os, &sh) == NULL)
1367
		errx(EXIT_FAILURE, "692 gelf_getshdr() failed: %s",
1409
		errx(EXIT_FAILURE, "gelf_getshdr() failed: %s",
1368
		    elf_errmsg(-1));
1410
		    elf_errmsg(-1));
1369
	sh.sh_addr	= 0;
1411
	sh.sh_addr	= 0;
1370
	sh.sh_addralign	= 1;
1412
	sh.sh_addralign	= 1;
Lines 1431-1444 add_section(struct elfcopy *ecp, const char *arg) Link Here
1431
	if (stat(fn, &sb) == -1)
1473
	if (stat(fn, &sb) == -1)
1432
		err(EXIT_FAILURE, "stat failed");
1474
		err(EXIT_FAILURE, "stat failed");
1433
	sa->size = sb.st_size;
1475
	sa->size = sb.st_size;
1434
	if ((sa->content = malloc(sa->size)) == NULL)
1476
	if (sa->size > 0) {
1435
		err(EXIT_FAILURE, "malloc failed");
1477
		if ((sa->content = malloc(sa->size)) == NULL)
1436
	if ((fp = fopen(fn, "r")) == NULL)
1478
			err(EXIT_FAILURE, "malloc failed");
1437
		err(EXIT_FAILURE, "can not open %s", fn);
1479
		if ((fp = fopen(fn, "r")) == NULL)
1438
	if (fread(sa->content, 1, sa->size, fp) == 0 ||
1480
			err(EXIT_FAILURE, "can not open %s", fn);
1439
	    ferror(fp))
1481
		if (fread(sa->content, 1, sa->size, fp) == 0 ||
1440
		err(EXIT_FAILURE, "fread failed");
1482
		    ferror(fp))
1441
	fclose(fp);
1483
			err(EXIT_FAILURE, "fread failed");
1484
		fclose(fp);
1485
	} else
1486
		sa->content = NULL;
1442
1487
1443
	STAILQ_INSERT_TAIL(&ecp->v_sadd, sa, sadd_list);
1488
	STAILQ_INSERT_TAIL(&ecp->v_sadd, sa, sadd_list);
1444
	ecp->flags |= SEC_ADD;
1489
	ecp->flags |= SEC_ADD;
(-)b/contrib/elftoolchain/elfcopy/segments.c (-6 / +10 lines)
Lines 34-40 Link Here
34
34
35
#include "elfcopy.h"
35
#include "elfcopy.h"
36
36
37
ELFTC_VCSID("$Id: segments.c 3269 2015-12-11 18:38:43Z kaiwang27 $");
37
ELFTC_VCSID("$Id: segments.c 3340 2016-01-17 15:00:56Z kaiwang27 $");
38
38
39
static void	insert_to_inseg_list(struct segment *seg, struct section *sec);
39
static void	insert_to_inseg_list(struct segment *seg, struct section *sec);
40
40
Lines 107-117 adjust_addr(struct elfcopy *ecp) Link Here
107
	TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
107
	TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
108
108
109
		/* Only adjust loadable section's address. */
109
		/* Only adjust loadable section's address. */
110
		if (!s->loadable || s->seg == NULL)
110
		if (!s->loadable)
111
			continue;
111
			continue;
112
112
113
		/* Apply global LMA adjustment. */
113
		/* Apply global LMA adjustment. */
114
		if (ecp->change_addr != 0)
114
		if (ecp->change_addr != 0 && s->seg != NULL)
115
			s->lma += ecp->change_addr;
115
			s->lma += ecp->change_addr;
116
116
117
		if (!s->pseudo) {
117
		if (!s->pseudo) {
Lines 135-141 adjust_addr(struct elfcopy *ecp) Link Here
135
	 */
135
	 */
136
	TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
136
	TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
137
137
138
		/* Only adjust loadable section's LMA. */
138
		/*
139
		 * Only loadable section that's inside a segment can have
140
		 * LMA adjusted.
141
		 */
139
		if (!s->loadable || s->seg == NULL)
142
		if (!s->loadable || s->seg == NULL)
140
			continue;
143
			continue;
141
144
Lines 173-179 adjust_addr(struct elfcopy *ecp) Link Here
173
		if (lma % s->align != 0)
176
		if (lma % s->align != 0)
174
			errx(EXIT_FAILURE, "The load address %#jx for "
177
			errx(EXIT_FAILURE, "The load address %#jx for "
175
			    "section %s is not aligned to %ju",
178
			    "section %s is not aligned to %ju",
176
			    (uintmax_t) lma, s->name, s->align);
179
			    (uintmax_t) lma, s->name, (uintmax_t) s->align);
177
180
178
		if (lma < s->lma) {
181
		if (lma < s->lma) {
179
			/* Move section to lower address. */
182
			/* Move section to lower address. */
Lines 214-220 adjust_addr(struct elfcopy *ecp) Link Here
214
				continue;
217
				continue;
215
			errx(EXIT_FAILURE, "The extent of segment containing "
218
			errx(EXIT_FAILURE, "The extent of segment containing "
216
			    "section %s overlaps with segment(%#jx,%#jx)",
219
			    "section %s overlaps with segment(%#jx,%#jx)",
217
			    s->name, seg->addr, seg->addr + seg->msz);
220
			    s->name, (uintmax_t) seg->addr,
221
			    (uintmax_t) (seg->addr + seg->msz));
218
		}
222
		}
219
223
220
		/*
224
		/*
(-)b/contrib/elftoolchain/elfcopy/symbols.c (-45 / +81 lines)
Lines 25-30 Link Here
25
 */
25
 */
26
26
27
#include <sys/param.h>
27
#include <sys/param.h>
28
#include <assert.h>
28
#include <err.h>
29
#include <err.h>
29
#include <fnmatch.h>
30
#include <fnmatch.h>
30
#include <stdio.h>
31
#include <stdio.h>
Lines 33-39 Link Here
33
34
34
#include "elfcopy.h"
35
#include "elfcopy.h"
35
36
36
ELFTC_VCSID("$Id: symbols.c 3222 2015-05-24 23:47:23Z kaiwang27 $");
37
ELFTC_VCSID("$Id: symbols.c 3376 2016-01-26 18:41:39Z emaste $");
38
39
/* Backwards compatibility for systems with older ELF definitions. */
40
#ifndef STB_GNU_UNIQUE
41
#define	STB_GNU_UNIQUE 10
42
#endif
43
37
44
38
/* Symbol table buffer structure. */
45
/* Symbol table buffer structure. */
39
struct symbuf {
46
struct symbuf {
Lines 79-85 static int lookup_exact_string(hash_head *hash, const char *buf, Link Here
79
static int	generate_symbols(struct elfcopy *ecp);
86
static int	generate_symbols(struct elfcopy *ecp);
80
static void	mark_reloc_symbols(struct elfcopy *ecp, size_t sc);
87
static void	mark_reloc_symbols(struct elfcopy *ecp, size_t sc);
81
static void	mark_section_group_symbols(struct elfcopy *ecp, size_t sc);
88
static void	mark_section_group_symbols(struct elfcopy *ecp, size_t sc);
82
static int	match_wildcard(const char *name, const char *pattern);
83
uint32_t	str_hash(const char *s);
89
uint32_t	str_hash(const char *s);
84
90
85
/* Convenient bit vector operation macros. */
91
/* Convenient bit vector operation macros. */
Lines 102-108 static int Link Here
102
is_global_symbol(unsigned char st_info)
108
is_global_symbol(unsigned char st_info)
103
{
109
{
104
110
105
	if (GELF_ST_BIND(st_info) == STB_GLOBAL)
111
	if (GELF_ST_BIND(st_info) == STB_GLOBAL ||
112
	    GELF_ST_BIND(st_info) == STB_GNU_UNIQUE)
106
		return (1);
113
		return (1);
107
114
108
	return (0);
115
	return (0);
Lines 190-201 is_remove_symbol(struct elfcopy *ecp, size_t sc, int i, GElf_Sym *s, Link Here
190
		SHN_UNDEF,	/* st_shndx */
197
		SHN_UNDEF,	/* st_shndx */
191
	};
198
	};
192
199
193
	if (lookup_symop_list(ecp, name, SYMOP_KEEP) != NULL)
194
		return (0);
195
196
	if (lookup_symop_list(ecp, name, SYMOP_STRIP) != NULL)
197
		return (1);
198
199
	/*
200
	/*
200
	 * Keep the first symbol if it is the special reserved symbol.
201
	 * Keep the first symbol if it is the special reserved symbol.
201
	 * XXX Should we generate one if it's missing?
202
	 * XXX Should we generate one if it's missing?
Lines 208-222 is_remove_symbol(struct elfcopy *ecp, size_t sc, int i, GElf_Sym *s, Link Here
208
	    ecp->secndx[s->st_shndx] == 0)
209
	    ecp->secndx[s->st_shndx] == 0)
209
		return (1);
210
		return (1);
210
211
212
	/* Keep the symbol if specified by command line option -K. */
213
	if (lookup_symop_list(ecp, name, SYMOP_KEEP) != NULL)
214
		return (0);
215
211
	if (ecp->strip == STRIP_ALL)
216
	if (ecp->strip == STRIP_ALL)
212
		return (1);
217
		return (1);
213
218
219
	/* Mark symbols used in relocation. */
214
	if (ecp->v_rel == NULL)
220
	if (ecp->v_rel == NULL)
215
		mark_reloc_symbols(ecp, sc);
221
		mark_reloc_symbols(ecp, sc);
216
222
223
	/* Mark symbols used in section groups. */
217
	if (ecp->v_grp == NULL)
224
	if (ecp->v_grp == NULL)
218
		mark_section_group_symbols(ecp, sc);
225
		mark_section_group_symbols(ecp, sc);
219
226
227
	/*
228
	 * Strip the symbol if specified by command line option -N,
229
	 * unless it's used in relocation.
230
	 */
231
	if (lookup_symop_list(ecp, name, SYMOP_STRIP) != NULL) {
232
		if (BIT_ISSET(ecp->v_rel, i)) {
233
			warnx("not stripping symbol `%s' because it is named"
234
			    " in a relocation", name);
235
			return (0);
236
		}
237
		return (1);
238
	}
239
220
	if (is_needed_symbol(ecp, i, s))
240
	if (is_needed_symbol(ecp, i, s))
221
		return (0);
241
		return (0);
222
242
Lines 565-572 generate_symbols(struct elfcopy *ecp) Link Here
565
		 * If the symbol is a STT_SECTION symbol, mark the section
585
		 * If the symbol is a STT_SECTION symbol, mark the section
566
		 * it points to.
586
		 * it points to.
567
		 */
587
		 */
568
		if (GELF_ST_TYPE(sym.st_info) == STT_SECTION)
588
		if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
589
		    sym.st_shndx < SHN_LORESERVE) {
590
			assert(ecp->secndx[sym.st_shndx] < (uint64_t)ecp->nos);
569
			BIT_SET(ecp->v_secsym, ecp->secndx[sym.st_shndx]);
591
			BIT_SET(ecp->v_secsym, ecp->secndx[sym.st_shndx]);
592
		}
570
	}
593
	}
571
594
572
	/*
595
	/*
Lines 861-866 add_to_symtab(struct elfcopy *ecp, const char *name, uint64_t st_value, Link Here
861
	 * It handles buffer growing, st_name calculating and st_shndx
884
	 * It handles buffer growing, st_name calculating and st_shndx
862
	 * updating for symbols with non-special section index.
885
	 * updating for symbols with non-special section index.
863
	 */
886
	 */
887
#define	_ST_NAME_EMPTY_l 0
888
#define	_ST_NAME_EMPTY_g -1
864
#define	_ADDSYM(B, SZ) do {						\
889
#define	_ADDSYM(B, SZ) do {						\
865
	if (sy_buf->B##SZ == NULL) {					\
890
	if (sy_buf->B##SZ == NULL) {					\
866
		sy_buf->B##SZ = malloc(sy_buf->B##cap *			\
891
		sy_buf->B##SZ = malloc(sy_buf->B##cap *			\
Lines 920-926 add_to_symtab(struct elfcopy *ecp, const char *name, uint64_t st_value, Link Here
920
			st_buf->B.sz += strlen(name) + 1;		\
945
			st_buf->B.sz += strlen(name) + 1;		\
921
		}							\
946
		}							\
922
	} else								\
947
	} else								\
923
		sy_buf->B##SZ[sy_buf->n##B##s].st_name = 0;		\
948
		sy_buf->B##SZ[sy_buf->n##B##s].st_name = 		\
949
		    (Elf##SZ##_Word)_ST_NAME_EMPTY_##B;			\
924
	sy_buf->n##B##s++;						\
950
	sy_buf->n##B##s++;						\
925
} while (0)
951
} while (0)
926
952
Lines 945-950 add_to_symtab(struct elfcopy *ecp, const char *name, uint64_t st_value, Link Here
945
	ecp->strtab->sz = st_buf->l.sz + st_buf->g.sz;
971
	ecp->strtab->sz = st_buf->l.sz + st_buf->g.sz;
946
972
947
#undef	_ADDSYM
973
#undef	_ADDSYM
974
#undef	_ST_NAME_EMPTY_l
975
#undef	_ST_NAME_EMPTY_g
948
}
976
}
949
977
950
void
978
void
Lines 961-970 finalize_external_symtab(struct elfcopy *ecp) Link Here
961
	sy_buf = ecp->symtab->buf;
989
	sy_buf = ecp->symtab->buf;
962
	st_buf = ecp->strtab->buf;
990
	st_buf = ecp->strtab->buf;
963
	for (i = 0; (size_t) i < sy_buf->ngs; i++) {
991
	for (i = 0; (size_t) i < sy_buf->ngs; i++) {
964
		if (ecp->oec == ELFCLASS32)
992
		if (ecp->oec == ELFCLASS32) {
965
			sy_buf->g32[i].st_name += st_buf->l.sz;
993
			if (sy_buf->g32[i].st_name == (Elf32_Word)-1)
966
		else
994
				sy_buf->g32[i].st_name = 0;
967
			sy_buf->g64[i].st_name += st_buf->l.sz;
995
			else
996
				sy_buf->g32[i].st_name += st_buf->l.sz;
997
		} else {
998
			if (sy_buf->g64[i].st_name == (Elf64_Word)-1)
999
				sy_buf->g64[i].st_name = 0;
1000
			else
1001
				sy_buf->g64[i].st_name += st_buf->l.sz;
1002
		}
968
	}
1003
	}
969
}
1004
}
970
1005
Lines 1105-1150 add_to_symop_list(struct elfcopy *ecp, const char *name, const char *newname, Link Here
1105
{
1140
{
1106
	struct symop *s;
1141
	struct symop *s;
1107
1142
1108
	if ((s = lookup_symop_list(ecp, name, ~0U)) == NULL) {
1143
	assert (name != NULL);
1109
		if ((s = calloc(1, sizeof(*s))) == NULL)
1144
	STAILQ_FOREACH(s, &ecp->v_symop, symop_list)
1110
			errx(EXIT_FAILURE, "not enough memory");
1145
		if (!strcmp(name, s->name))
1111
		s->name = name;
1146
			goto found;
1112
		if (op == SYMOP_REDEF)
1113
			s->newname = newname;
1114
	}
1115
1147
1116
	s->op |= op;
1148
	if ((s = calloc(1, sizeof(*s))) == NULL)
1149
		errx(EXIT_FAILURE, "not enough memory");
1117
	STAILQ_INSERT_TAIL(&ecp->v_symop, s, symop_list);
1150
	STAILQ_INSERT_TAIL(&ecp->v_symop, s, symop_list);
1118
}
1151
	s->name = name;
1119
1152
found:
1120
static int
1153
	if (op == SYMOP_REDEF)
1121
match_wildcard(const char *name, const char *pattern)
1154
		s->newname = newname;
1122
{
1155
	s->op |= op;
1123
	int reverse, match;
1124
1125
	reverse = 0;
1126
	if (*pattern == '!') {
1127
		reverse = 1;
1128
		pattern++;
1129
	}
1130
1131
	match = 0;
1132
	if (!fnmatch(pattern, name, 0))
1133
		match = 1;
1134
1135
	return (reverse ? !match : match);
1136
}
1156
}
1137
1157
1138
struct symop *
1158
struct symop *
1139
lookup_symop_list(struct elfcopy *ecp, const char *name, unsigned int op)
1159
lookup_symop_list(struct elfcopy *ecp, const char *name, unsigned int op)
1140
{
1160
{
1141
	struct symop *s;
1161
	struct symop *s, *ret;
1162
	const char *pattern;
1142
1163
1143
	STAILQ_FOREACH(s, &ecp->v_symop, symop_list) {
1164
	STAILQ_FOREACH(s, &ecp->v_symop, symop_list) {
1144
		if (name == NULL || !strcmp(name, s->name) ||
1165
		if ((s->op & op) == 0)
1145
		    ((ecp->flags & WILDCARD) && match_wildcard(name, s->name)))
1166
			continue;
1146
			if ((s->op & op) != 0)
1167
		if (name == NULL || !strcmp(name, s->name))
1147
				return (s);
1168
				return (s);
1169
		if ((ecp->flags & WILDCARD) == 0)
1170
			continue;
1171
1172
		/* Handle wildcards. */
1173
		pattern = s->name;
1174
		if (pattern[0] == '!') {
1175
			/* Negative match. */
1176
			pattern++;
1177
			ret = NULL;
1178
		} else {
1179
			/* Regular wildcard match. */
1180
			ret = s;
1181
		}
1182
		if (!fnmatch(pattern, name, 0))
1183
			return (ret);
1148
	}
1184
	}
1149
1185
1150
	return (NULL);
1186
	return (NULL);
(-)b/contrib/elftoolchain/elfdump/elfdump.c (-128 / +242 lines)
Lines 50-56 Link Here
50
50
51
#include "_elftc.h"
51
#include "_elftc.h"
52
52
53
ELFTC_VCSID("$Id: elfdump.c 3250 2015-10-06 13:56:15Z emaste $");
53
ELFTC_VCSID("$Id: elfdump.c 3391 2016-02-05 19:43:01Z emaste $");
54
54
55
#if defined(ELFTC_NEED_ELF_NOTE_DEFINITION)
55
#if defined(ELFTC_NEED_ELF_NOTE_DEFINITION)
56
#include "native-elf-format.h"
56
#include "native-elf-format.h"
Lines 155-231 le32dec(const void *pp) Link Here
155
static const char *
155
static const char *
156
d_tags(uint64_t tag)
156
d_tags(uint64_t tag)
157
{
157
{
158
	static char unknown_buf[64];
159
158
	switch (tag) {
160
	switch (tag) {
159
	case 0: return "DT_NULL";
161
	case DT_NULL:		return "DT_NULL";
160
	case 1: return "DT_NEEDED";
162
	case DT_NEEDED:		return "DT_NEEDED";
161
	case 2: return "DT_PLTRELSZ";
163
	case DT_PLTRELSZ:	return "DT_PLTRELSZ";
162
	case 3: return "DT_PLTGOT";
164
	case DT_PLTGOT:		return "DT_PLTGOT";
163
	case 4: return "DT_HASH";
165
	case DT_HASH:		return "DT_HASH";
164
	case 5: return "DT_STRTAB";
166
	case DT_STRTAB:		return "DT_STRTAB";
165
	case 6: return "DT_SYMTAB";
167
	case DT_SYMTAB:		return "DT_SYMTAB";
166
	case 7: return "DT_RELA";
168
	case DT_RELA:		return "DT_RELA";
167
	case 8: return "DT_RELASZ";
169
	case DT_RELASZ:		return "DT_RELASZ";
168
	case 9: return "DT_RELAENT";
170
	case DT_RELAENT:	return "DT_RELAENT";
169
	case 10: return "DT_STRSZ";
171
	case DT_STRSZ:		return "DT_STRSZ";
170
	case 11: return "DT_SYMENT";
172
	case DT_SYMENT:		return "DT_SYMENT";
171
	case 12: return "DT_INIT";
173
	case DT_INIT:		return "DT_INIT";
172
	case 13: return "DT_FINI";
174
	case DT_FINI:		return "DT_FINI";
173
	case 14: return "DT_SONAME";
175
	case DT_SONAME:		return "DT_SONAME";
174
	case 15: return "DT_RPATH";
176
	case DT_RPATH:		return "DT_RPATH";
175
	case 16: return "DT_SYMBOLIC";
177
	case DT_SYMBOLIC:	return "DT_SYMBOLIC";
176
	case 17: return "DT_REL";
178
	case DT_REL:		return "DT_REL";
177
	case 18: return "DT_RELSZ";
179
	case DT_RELSZ:		return "DT_RELSZ";
178
	case 19: return "DT_RELENT";
180
	case DT_RELENT:		return "DT_RELENT";
179
	case 20: return "DT_PLTREL";
181
	case DT_PLTREL:		return "DT_PLTREL";
180
	case 21: return "DT_DEBUG";
182
	case DT_DEBUG:		return "DT_DEBUG";
181
	case 22: return "DT_TEXTREL";
183
	case DT_TEXTREL:	return "DT_TEXTREL";
182
	case 23: return "DT_JMPREL";
184
	case DT_JMPREL:		return "DT_JMPREL";
183
	case 24: return "DT_BIND_NOW";
185
	case DT_BIND_NOW:	return "DT_BIND_NOW";
184
	case 25: return "DT_INIT_ARRAY";
186
	case DT_INIT_ARRAY:	return "DT_INIT_ARRAY";
185
	case 26: return "DT_FINI_ARRAY";
187
	case DT_FINI_ARRAY:	return "DT_FINI_ARRAY";
186
	case 27: return "DT_INIT_ARRAYSZ";
188
	case DT_INIT_ARRAYSZ:	return "DT_INIT_ARRAYSZ";
187
	case 28: return "DT_FINI_ARRAYSZ";
189
	case DT_FINI_ARRAYSZ:	return "DT_FINI_ARRAYSZ";
188
	case 29: return "DT_RUNPATH";
190
	case DT_RUNPATH:	return "DT_RUNPATH";
189
	case 30: return "DT_FLAGS";
191
	case DT_FLAGS:		return "DT_FLAGS";
190
	case 32: return "DT_PREINIT_ARRAY"; /* XXX: DT_ENCODING */
192
	case DT_PREINIT_ARRAY:	return "DT_PREINIT_ARRAY"; /* XXX DT_ENCODING */
191
	case 33: return "DT_PREINIT_ARRAYSZ";
193
	case DT_PREINIT_ARRAYSZ:return "DT_PREINIT_ARRAYSZ";
192
	/* 0x6000000D - 0x6ffff000 operating system-specific semantics */
194
	/* 0x6000000D - 0x6ffff000 operating system-specific semantics */
193
	case 0x6ffffdf5: return "DT_GNU_PRELINKED";
195
	case 0x6ffffdf5:	return "DT_GNU_PRELINKED";
194
	case 0x6ffffdf6: return "DT_GNU_CONFLICTSZ";
196
	case 0x6ffffdf6:	return "DT_GNU_CONFLICTSZ";
195
	case 0x6ffffdf7: return "DT_GNU_LIBLISTSZ";
197
	case 0x6ffffdf7:	return "DT_GNU_LIBLISTSZ";
196
	case 0x6ffffdf8: return "DT_SUNW_CHECKSUM";
198
	case 0x6ffffdf8:	return "DT_SUNW_CHECKSUM";
197
	case 0x6ffffdf9: return "DT_PLTPADSZ";
199
	case DT_PLTPADSZ:	return "DT_PLTPADSZ";
198
	case 0x6ffffdfa: return "DT_MOVEENT";
200
	case DT_MOVEENT:	return "DT_MOVEENT";
199
	case 0x6ffffdfb: return "DT_MOVESZ";
201
	case DT_MOVESZ:		return "DT_MOVESZ";
200
	case 0x6ffffdfc: return "DT_FEATURE";
202
	case 0x6ffffdfc:	return "DT_FEATURE";
201
	case 0x6ffffdfd: return "DT_POSFLAG_1";
203
	case DT_POSFLAG_1:	return "DT_POSFLAG_1";
202
	case 0x6ffffdfe: return "DT_SYMINSZ";
204
	case DT_SYMINSZ:	return "DT_SYMINSZ";
203
	case 0x6ffffdff: return "DT_SYMINENT (DT_VALRNGHI)";
205
	case DT_SYMINENT:	return "DT_SYMINENT (DT_VALRNGHI)";
204
	case 0x6ffffe00: return "DT_ADDRRNGLO";
206
	case DT_ADDRRNGLO:	return "DT_ADDRRNGLO";
205
	case 0x6ffffef5: return "DT_GNU_HASH";
207
	case DT_GNU_HASH:	return "DT_GNU_HASH";
206
	case 0x6ffffef8: return "DT_GNU_CONFLICT";
208
	case 0x6ffffef8:	return "DT_GNU_CONFLICT";
207
	case 0x6ffffef9: return "DT_GNU_LIBLIST";
209
	case 0x6ffffef9:	return "DT_GNU_LIBLIST";
208
	case 0x6ffffefa: return "DT_CONFIG";
210
	case 0x6ffffefa:	return "DT_CONFIG";
209
	case 0x6ffffefb: return "DT_DEPAUDIT";
211
	case 0x6ffffefb:	return "DT_DEPAUDIT";
210
	case 0x6ffffefc: return "DT_AUDIT";
212
	case 0x6ffffefc:	return "DT_AUDIT";
211
	case 0x6ffffefd: return "DT_PLTPAD";
213
	case 0x6ffffefd:	return "DT_PLTPAD";
212
	case 0x6ffffefe: return "DT_MOVETAB";
214
	case 0x6ffffefe:	return "DT_MOVETAB";
213
	case 0x6ffffeff: return "DT_SYMINFO (DT_ADDRRNGHI)";
215
	case DT_SYMINFO:	return "DT_SYMINFO (DT_ADDRRNGHI)";
214
	case 0x6ffffff9: return "DT_RELACOUNT";
216
	case DT_RELACOUNT:	return "DT_RELACOUNT";
215
	case 0x6ffffffa: return "DT_RELCOUNT";
217
	case DT_RELCOUNT:	return "DT_RELCOUNT";
216
	case 0x6ffffffb: return "DT_FLAGS_1";
218
	case DT_FLAGS_1:	return "DT_FLAGS_1";
217
	case 0x6ffffffc: return "DT_VERDEF";
219
	case DT_VERDEF:		return "DT_VERDEF";
218
	case 0x6ffffffd: return "DT_VERDEFNUM";
220
	case DT_VERDEFNUM:	return "DT_VERDEFNUM";
219
	case 0x6ffffffe: return "DT_VERNEED";
221
	case DT_VERNEED:	return "DT_VERNEED";
220
	case 0x6fffffff: return "DT_VERNEEDNUM";
222
	case DT_VERNEEDNUM:	return "DT_VERNEEDNUM";
221
	case 0x6ffffff0: return "DT_GNU_VERSYM";
223
	case 0x6ffffff0:	return "DT_GNU_VERSYM";
222
	/* 0x70000000 - 0x7fffffff processor-specific semantics */
224
	/* 0x70000000 - 0x7fffffff processor-specific semantics */
223
	case 0x70000000: return "DT_IA_64_PLT_RESERVE";
225
	case 0x70000000:	return "DT_IA_64_PLT_RESERVE";
224
	case 0x7ffffffd: return "DT_SUNW_AUXILIARY";
226
	case 0x7ffffffd:	return "DT_SUNW_AUXILIARY";
225
	case 0x7ffffffe: return "DT_SUNW_USED";
227
	case 0x7ffffffe:	return "DT_SUNW_USED";
226
	case 0x7fffffff: return "DT_SUNW_FILTER";
228
	case 0x7fffffff:	return "DT_SUNW_FILTER";
227
	default: return "ERROR: TAG NOT DEFINED";
228
	}
229
	}
230
231
	snprintf(unknown_buf, sizeof(unknown_buf),
232
		"<unknown: %#llx>", (unsigned long long)tag);
233
	return (unknown_buf);
229
}
234
}
230
235
231
static const char *
236
static const char *
Lines 313-354 sh_name(struct elfdump *ed, int ndx) Link Here
313
318
314
/* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
319
/* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
315
static const char *
320
static const char *
316
sh_types(u_int64_t sht) {
321
sh_types(uint64_t mach, uint64_t sht) {
317
	switch (sht) {
322
	static char unknown_buf[64];
318
	case 0:	return "SHT_NULL";
323
319
	case 1: return "SHT_PROGBITS";
324
	if (sht < 0x60000000) {
320
	case 2: return "SHT_SYMTAB";
325
		switch (sht) {
321
	case 3: return "SHT_STRTAB";
326
		case SHT_NULL:		return "SHT_NULL";
322
	case 4: return "SHT_RELA";
327
		case SHT_PROGBITS:	return "SHT_PROGBITS";
323
	case 5: return "SHT_HASH";
328
		case SHT_SYMTAB:	return "SHT_SYMTAB";
324
	case 6: return "SHT_DYNAMIC";
329
		case SHT_STRTAB:	return "SHT_STRTAB";
325
	case 7: return "SHT_NOTE";
330
		case SHT_RELA:		return "SHT_RELA";
326
	case 8: return "SHT_NOBITS";
331
		case SHT_HASH:		return "SHT_HASH";
327
	case 9: return "SHT_REL";
332
		case SHT_DYNAMIC:	return "SHT_DYNAMIC";
328
	case 10: return "SHT_SHLIB";
333
		case SHT_NOTE:		return "SHT_NOTE";
329
	case 11: return "SHT_DYNSYM";
334
		case SHT_NOBITS:	return "SHT_NOBITS";
330
	case 14: return "SHT_INIT_ARRAY";
335
		case SHT_REL:		return "SHT_REL";
331
	case 15: return "SHT_FINI_ARRAY";
336
		case SHT_SHLIB:		return "SHT_SHLIB";
332
	case 16: return "SHT_PREINIT_ARRAY";
337
		case SHT_DYNSYM:	return "SHT_DYNSYM";
333
	case 17: return "SHT_GROUP";
338
		case SHT_INIT_ARRAY:	return "SHT_INIT_ARRAY";
334
	case 18: return "SHT_SYMTAB_SHNDX";
339
		case SHT_FINI_ARRAY:	return "SHT_FINI_ARRAY";
335
	/* 0x60000000 - 0x6fffffff operating system-specific semantics */
340
		case SHT_PREINIT_ARRAY:	return "SHT_PREINIT_ARRAY";
336
	case 0x6ffffff0: return "XXX:VERSYM";
341
		case SHT_GROUP:		return "SHT_GROUP";
337
	case 0x6ffffff4: return "SHT_SUNW_dof";
342
		case SHT_SYMTAB_SHNDX:	return "SHT_SYMTAB_SHNDX";
338
	case 0x6ffffff6: return "SHT_GNU_HASH";
343
		}
339
	case 0x6ffffff7: return "SHT_GNU_LIBLIST";
344
	} else if (sht < 0x70000000) {
340
	case 0x6ffffffc: return "XXX:VERDEF";
345
		/* 0x60000000-0x6fffffff operating system-specific semantics */
341
	case 0x6ffffffd: return "SHT_SUNW(GNU)_verdef";
346
		switch (sht) {
342
	case 0x6ffffffe: return "SHT_SUNW(GNU)_verneed";
347
		case 0x6ffffff0:	return "XXX:VERSYM";
343
	case 0x6fffffff: return "SHT_SUNW(GNU)_versym";
348
		case SHT_SUNW_dof:	return "SHT_SUNW_dof";
344
	/* 0x70000000 - 0x7fffffff processor-specific semantics */
349
		case SHT_GNU_HASH:	return "SHT_GNU_HASH";
345
	case 0x70000000: return "SHT_IA_64_EXT";
350
		case 0x6ffffff7:	return "SHT_GNU_LIBLIST";
346
	case 0x70000001: return "SHT_IA_64_UNWIND";
351
		case 0x6ffffffc:	return "XXX:VERDEF";
347
	case 0x7ffffffd: return "XXX:AUXILIARY";
352
		case SHT_SUNW_verdef:	return "SHT_SUNW(GNU)_verdef";
348
	case 0x7fffffff: return "XXX:FILTER";
353
		case SHT_SUNW_verneed:	return "SHT_SUNW(GNU)_verneed";
349
	/* 0x80000000 - 0xffffffff application programs */
354
		case SHT_SUNW_versym:	return "SHT_SUNW(GNU)_versym";
350
	default: return "ERROR: SHT NOT DEFINED";
355
		}
356
	} else if (sht < 0x80000000) {
357
		/* 0x70000000 - 0x7fffffff processor-specific semantics */
358
		switch (mach) {
359
		case EM_ARM:
360
			switch (sht) {
361
			case SHT_ARM_EXIDX: return "SHT_ARM_EXIDX";
362
			case SHT_ARM_PREEMPTMAP: return "SHT_ARM_PREEMPTMAP";
363
			case SHT_ARM_ATTRIBUTES: return "SHT_ARM_ATTRIBUTES";
364
			case SHT_ARM_DEBUGOVERLAY:
365
			    return "SHT_ARM_DEBUGOVERLAY";
366
			case SHT_ARM_OVERLAYSECTION:
367
			    return "SHT_ARM_OVERLAYSECTION";
368
			}
369
			break;
370
		case EM_IA_64:
371
			switch (sht) {
372
			case 0x70000000: return "SHT_IA_64_EXT";
373
			case 0x70000001: return "SHT_IA_64_UNWIND";
374
			}
375
			break;
376
		case EM_MIPS:
377
			switch (sht) {
378
			case SHT_MIPS_REGINFO: return "SHT_MIPS_REGINFO";
379
			case SHT_MIPS_OPTIONS: return "SHT_MIPS_OPTIONS";
380
			case SHT_MIPS_ABIFLAGS: return "SHT_MIPS_ABIFLAGS";
381
			}
382
			break;
383
		}
384
		switch (sht) {
385
		case 0x7ffffffd: return "XXX:AUXILIARY";
386
		case 0x7fffffff: return "XXX:FILTER";
387
		}
351
	}
388
	}
389
	/* 0x80000000 - 0xffffffff application programs */
390
391
	snprintf(unknown_buf, sizeof(unknown_buf),
392
		"<unknown: %#llx>", (unsigned long long)sht);
393
	return (unknown_buf);
352
}
394
}
353
395
354
/*
396
/*
Lines 390-411 sh_flags(uint64_t shf) Link Here
390
	return (flg);
432
	return (flg);
391
}
433
}
392
434
393
static const char *st_types[] = {
435
static const char *
394
	"STT_NOTYPE", "STT_OBJECT", "STT_FUNC", "STT_SECTION", "STT_FILE",
436
st_type(unsigned int mach, unsigned int type)
395
	"STT_COMMON", "STT_TLS"
437
{
396
};
438
	static char s_type[32];
439
440
	switch (type) {
441
	case STT_NOTYPE: return "STT_NOTYPE";
442
	case STT_OBJECT: return "STT_OBJECT";
443
	case STT_FUNC: return "STT_FUNC";
444
	case STT_SECTION: return "STT_SECTION";
445
	case STT_FILE: return "STT_FILE";
446
	case STT_COMMON: return "STT_COMMON";
447
	case STT_TLS: return "STT_TLS";
448
	case 13:
449
		if (mach == EM_SPARCV9)
450
			return "STT_SPARC_REGISTER";
451
		break;
452
	}
453
	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
454
	return (s_type);
455
}
397
456
398
static const char *st_types_S[] = {
457
static const char *
399
	"NOTY", "OBJT", "FUNC", "SECT", "FILE"
458
st_type_S(unsigned int type)
400
};
459
{
460
	static char s_type[32];
401
461
402
static const char *st_bindings[] = {
462
	switch (type) {
403
	"STB_LOCAL", "STB_GLOBAL", "STB_WEAK"
463
	case STT_NOTYPE: return "NOTY";
404
};
464
	case STT_OBJECT: return "OBJT";
465
	case STT_FUNC: return "FUNC";
466
	case STT_SECTION: return "SECT";
467
	case STT_FILE: return "FILE";
468
	}
469
	snprintf(s_type, sizeof(s_type), "<unknown: %#x>", type);
470
	return (s_type);
471
}
405
472
406
static const char *st_bindings_S[] = {
473
static const char *
407
	"LOCL", "GLOB", "WEAK"
474
st_bindings(unsigned int sbind)
408
};
475
{
476
	static char s_sbind[32];
477
478
	switch (sbind) {
479
	case STB_LOCAL: return "STB_LOCAL";
480
	case STB_GLOBAL: return "STB_GLOBAL";
481
	case STB_WEAK: return "STB_WEAK";
482
	case STB_GNU_UNIQUE: return "STB_GNU_UNIQUE";
483
	default:
484
		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
485
			return "OS";
486
		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
487
			return "PROC";
488
		else
489
			snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>",
490
			    sbind);
491
		return (s_sbind);
492
	}
493
}
494
495
static const char *
496
st_bindings_S(unsigned int sbind)
497
{
498
	static char s_sbind[32];
499
500
	switch (sbind) {
501
	case STB_LOCAL: return "LOCL";
502
	case STB_GLOBAL: return "GLOB";
503
	case STB_WEAK: return "WEAK";
504
	case STB_GNU_UNIQUE: return "UNIQ";
505
	default:
506
		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
507
			return "OS";
508
		else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC)
509
			return "PROC";
510
		else
511
			snprintf(s_sbind, sizeof(s_sbind), "<%#x>",
512
			    sbind);
513
		return (s_sbind);
514
	}
515
}
409
516
410
static unsigned char st_others[] = {
517
static unsigned char st_others[] = {
411
	'D', 'I', 'H', 'P'
518
	'D', 'I', 'H', 'P'
Lines 426-432 r_type(unsigned int mach, unsigned int type) Link Here
426
		case 4: return "R_386_PLT32";
533
		case 4: return "R_386_PLT32";
427
		case 5: return "R_386_COPY";
534
		case 5: return "R_386_COPY";
428
		case 6: return "R_386_GLOB_DAT";
535
		case 6: return "R_386_GLOB_DAT";
429
		case 7: return "R_386_JMP_SLOT";
536
		case 7: return "R_386_JUMP_SLOT";
430
		case 8: return "R_386_RELATIVE";
537
		case 8: return "R_386_RELATIVE";
431
		case 9: return "R_386_GOTOFF";
538
		case 9: return "R_386_GOTOFF";
432
		case 10: return "R_386_GOTPC";
539
		case 10: return "R_386_GOTPC";
Lines 769-775 r_type(unsigned int mach, unsigned int type) Link Here
769
		case 4: return "R_X86_64_PLT32";
876
		case 4: return "R_X86_64_PLT32";
770
		case 5: return "R_X86_64_COPY";
877
		case 5: return "R_X86_64_COPY";
771
		case 6: return "R_X86_64_GLOB_DAT";
878
		case 6: return "R_X86_64_GLOB_DAT";
772
		case 7: return "R_X86_64_JMP_SLOT";
879
		case 7: return "R_X86_64_JUMP_SLOT";
773
		case 8: return "R_X86_64_RELATIVE";
880
		case 8: return "R_X86_64_RELATIVE";
774
		case 9: return "R_X86_64_GOTPCREL";
881
		case 9: return "R_X86_64_GOTPCREL";
775
		case 10: return "R_X86_64_32";
882
		case 10: return "R_X86_64_32";
Lines 1608-1614 elf_print_shdr(struct elfdump *ed) Link Here
1608
			else
1715
			else
1609
				PRT("  sh_flags:   0\n");
1716
				PRT("  sh_flags:   0\n");
1610
			PRT("    sh_size:      %#-14jx", (uintmax_t)s->sz);
1717
			PRT("    sh_size:      %#-14jx", (uintmax_t)s->sz);
1611
			PRT("  sh_type:    [ %s ]\n", sh_types(s->type));
1718
			PRT("  sh_type:    [ %s ]\n",
1719
			    sh_types(ed->ehdr.e_machine, s->type));
1612
			PRT("    sh_offset:    %#-14jx", (uintmax_t)s->off);
1720
			PRT("    sh_offset:    %#-14jx", (uintmax_t)s->off);
1613
			PRT("  sh_entsize: %#jx\n", (uintmax_t)s->entsize);
1721
			PRT("  sh_entsize: %#jx\n", (uintmax_t)s->entsize);
1614
			PRT("    sh_link:      %-14u", s->link);
1722
			PRT("    sh_link:      %-14u", s->link);
Lines 1618-1624 elf_print_shdr(struct elfdump *ed) Link Here
1618
			PRT("\n");
1726
			PRT("\n");
1619
			PRT("entry: %ju\n", (uintmax_t)i);
1727
			PRT("entry: %ju\n", (uintmax_t)i);
1620
			PRT("\tsh_name: %s\n", s->name);
1728
			PRT("\tsh_name: %s\n", s->name);
1621
			PRT("\tsh_type: %s\n", sh_types(s->type));
1729
			PRT("\tsh_type: %s\n",
1730
			    sh_types(ed->ehdr.e_machine, s->type));
1622
			PRT("\tsh_flags: %s\n", sh_flags(s->flags));
1731
			PRT("\tsh_flags: %s\n", sh_flags(s->flags));
1623
			PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
1732
			PRT("\tsh_addr: %#jx\n", (uintmax_t)s->addr);
1624
			PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
1733
			PRT("\tsh_offset: %ju\n", (uintmax_t)s->off);
Lines 1745-1752 elf_print_symtab(struct elfdump *ed, int i) Link Here
1745
				PRT("0x%8.8jx  ", (uintmax_t)sym.st_size);
1854
				PRT("0x%8.8jx  ", (uintmax_t)sym.st_size);
1746
			else
1855
			else
1747
				PRT("0x%12.12jx  ", (uintmax_t)sym.st_size);
1856
				PRT("0x%12.12jx  ", (uintmax_t)sym.st_size);
1748
			PRT("%s ", st_types_S[GELF_ST_TYPE(sym.st_info)]);
1857
			PRT("%s ", st_type_S(GELF_ST_TYPE(sym.st_info)));
1749
			PRT("%s  ", st_bindings_S[GELF_ST_BIND(sym.st_info)]);
1858
			PRT("%s  ", st_bindings_S(GELF_ST_BIND(sym.st_info)));
1750
			PRT("%c  ", st_others[sym.st_other]);
1859
			PRT("%c  ", st_others[sym.st_other]);
1751
			PRT("%3u ", (vs == NULL ? 0 : vs[j]));
1860
			PRT("%3u ", (vs == NULL ? 0 : vs[j]));
1752
			PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
1861
			PRT("%-11.11s ", sh_name(ed, sym.st_shndx));
Lines 1757-1764 elf_print_symtab(struct elfdump *ed, int i) Link Here
1757
			PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
1866
			PRT("\tst_value: %#jx\n", (uintmax_t)sym.st_value);
1758
			PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
1867
			PRT("\tst_size: %ju\n", (uintmax_t)sym.st_size);
1759
			PRT("\tst_info: %s %s\n",
1868
			PRT("\tst_info: %s %s\n",
1760
			    st_types[GELF_ST_TYPE(sym.st_info)],
1869
			    st_type(ed->ehdr.e_machine,
1761
			    st_bindings[GELF_ST_BIND(sym.st_info)]);
1870
			    GELF_ST_TYPE(sym.st_info)),
1871
			    st_bindings(GELF_ST_BIND(sym.st_info)));
1762
			PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
1872
			PRT("\tst_shndx: %ju\n", (uintmax_t)sym.st_shndx);
1763
		}
1873
		}
1764
	}
1874
	}
Lines 2173-2183 elf_print_got_section(struct elfdump *ed, struct section *s) Link Here
2173
		for(i = 0; i < len; i++) {
2283
		for(i = 0; i < len; i++) {
2174
			PRT("[%5.5d]  ", i);
2284
			PRT("[%5.5d]  ", i);
2175
			if (ed->ec == ELFCLASS32) {
2285
			if (ed->ec == ELFCLASS32) {
2176
				PRT("%-8.8jx  ", s->addr + i * s->entsize);
2286
				PRT("%-8.8jx  ",
2287
				    (uintmax_t) (s->addr + i * s->entsize));
2177
				PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
2288
				PRT("%-8.8x ", *((uint32_t *)dst.d_buf + i));
2178
			} else {
2289
			} else {
2179
				PRT("%-16.16jx  ", s->addr + i * s->entsize);
2290
				PRT("%-16.16jx  ",
2180
				PRT("%-16.16jx  ", *((uint64_t *)dst.d_buf + i));
2291
				    (uintmax_t) (s->addr + i * s->entsize));
2292
				PRT("%-16.16jx  ",
2293
				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
2181
			}
2294
			}
2182
			PRT("%-18s ", r_type(ed->ehdr.e_machine,
2295
			PRT("%-18s ", r_type(ed->ehdr.e_machine,
2183
				GELF_R_TYPE(got[i].u_r.rel.r_info)));
2296
				GELF_R_TYPE(got[i].u_r.rel.r_info)));
Lines 2198-2204 elf_print_got_section(struct elfdump *ed, struct section *s) Link Here
2198
			if (ed->ec == ELFCLASS32)
2311
			if (ed->ec == ELFCLASS32)
2199
				PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
2312
				PRT("\t%#x\n", *((uint32_t *)dst.d_buf + i));
2200
			else
2313
			else
2201
				PRT("\t%#jx\n", *((uint64_t *)dst.d_buf + i));
2314
				PRT("\t%#jx\n",
2315
				    (uintmax_t) *((uint64_t *)dst.d_buf + i));
2202
		}
2316
		}
2203
	}
2317
	}
2204
}
2318
}
(-)b/contrib/elftoolchain/elfdump/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/libdwarf/_libdwarf.h (-5 / +5 lines)
Lines 24-30 Link Here
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
25
 * SUCH DAMAGE.
26
 *
26
 *
27
 * $Id: _libdwarf.h 3164 2015-02-19 01:20:12Z kaiwang27 $
27
 * $Id: _libdwarf.h 3298 2016-01-09 15:43:31Z jkoshy $
28
 */
28
 */
29
29
30
#ifndef	__LIBDWARF_H_
30
#ifndef	__LIBDWARF_H_
Lines 92-99 extern struct _libdwarf_globals _libdwarf; Link Here
92
typedef struct _Dwarf_CU *Dwarf_CU;
92
typedef struct _Dwarf_CU *Dwarf_CU;
93
93
94
struct _Dwarf_AttrDef {
94
struct _Dwarf_AttrDef {
95
	uint64_t	ad_attrib;		/* DW_AT_XXX */
95
	Dwarf_Half	ad_attrib;		/* DW_AT_XXX */
96
	uint64_t	ad_form;		/* DW_FORM_XXX */
96
	Dwarf_Half	ad_form;		/* DW_FORM_XXX */
97
	uint64_t	ad_offset;		/* Offset in abbrev section. */
97
	uint64_t	ad_offset;		/* Offset in abbrev section. */
98
	STAILQ_ENTRY(_Dwarf_AttrDef) ad_next;	/* Next attribute define. */
98
	STAILQ_ENTRY(_Dwarf_AttrDef) ad_next;	/* Next attribute define. */
99
};
99
};
Lines 102-109 struct _Dwarf_Attribute { Link Here
102
	Dwarf_Die		at_die;		/* Ptr to containing DIE. */
102
	Dwarf_Die		at_die;		/* Ptr to containing DIE. */
103
	Dwarf_Die		at_refdie;	/* Ptr to reference DIE. */
103
	Dwarf_Die		at_refdie;	/* Ptr to reference DIE. */
104
	uint64_t		at_offset;	/* Offset in info section. */
104
	uint64_t		at_offset;	/* Offset in info section. */
105
	uint64_t		at_attrib;	/* DW_AT_XXX */
105
	Dwarf_Half		at_attrib;	/* DW_AT_XXX */
106
	uint64_t		at_form;	/* DW_FORM_XXX */
106
	Dwarf_Half		at_form;	/* DW_FORM_XXX */
107
	int			at_indirect;	/* Has indirect form. */
107
	int			at_indirect;	/* Has indirect form. */
108
	union {
108
	union {
109
		uint64_t	u64;		/* Unsigned value. */
109
		uint64_t	u64;		/* Unsigned value. */
(-)b/contrib/elftoolchain/libdwarf/dwarf.3 (-4 / +2 lines)
Lines 21-27 Link Here
21
.\" out of the use of this software, even if advised of the possibility of
21
.\" out of the use of this software, even if advised of the possibility of
22
.\" such damage.
22
.\" such damage.
23
.\"
23
.\"
24
.\" $Id: dwarf.3 3195 2015-05-12 17:22:19Z emaste $
24
.\" $Id: dwarf.3 3295 2016-01-08 22:08:10Z jkoshy $
25
.\"
25
.\"
26
.Dd December 21, 2014
26
.Dd December 21, 2014
27
.Os
27
.Os
Lines 110-118 A pointer to an error handling function. Link Here
110
.It Vt Dwarf_Line
110
.It Vt Dwarf_Line
111
A descriptor for a source line.
111
A descriptor for a source line.
112
.It Vt Dwarf_Off
112
.It Vt Dwarf_Off
113
An unsigned file offset, corresponding to an
113
An unsigned file offset.
114
.Vt off_t
115
type supported by the underlying operating system.
116
.It Vt Dwarf_P_Expr
114
.It Vt Dwarf_P_Expr
117
A descriptor for a location expression.
115
A descriptor for a location expression.
118
.It Vt Dwarf_Ptr
116
.It Vt Dwarf_Ptr
(-)b/contrib/elftoolchain/libdwarf/dwarf_str.c (-4 / +4 lines)
Lines 26-32 Link Here
26
26
27
#include "_libdwarf.h"
27
#include "_libdwarf.h"
28
28
29
ELFTC_VCSID("$Id: dwarf_str.c 2075 2011-10-27 03:47:28Z jkoshy $");
29
ELFTC_VCSID("$Id: dwarf_str.c 3295 2016-01-08 22:08:10Z jkoshy $");
30
30
31
int
31
int
32
dwarf_get_str(Dwarf_Debug dbg, Dwarf_Off offset, char **string,
32
dwarf_get_str(Dwarf_Debug dbg, Dwarf_Off offset, char **string,
Lines 34-40 dwarf_get_str(Dwarf_Debug dbg, Dwarf_Off offset, char **string, Link Here
34
{
34
{
35
	Dwarf_Section *ds;
35
	Dwarf_Section *ds;
36
36
37
	if (dbg == NULL || offset < 0 || string == NULL || ret_strlen == NULL) {
37
	if (dbg == NULL || string == NULL || ret_strlen == NULL) {
38
		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
38
		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
39
		return (DW_DLV_ERROR);
39
		return (DW_DLV_ERROR);
40
	}
40
	}
Lines 45-56 dwarf_get_str(Dwarf_Debug dbg, Dwarf_Off offset, char **string, Link Here
45
		return (DW_DLV_NO_ENTRY);
45
		return (DW_DLV_NO_ENTRY);
46
	}
46
	}
47
47
48
	if ((Dwarf_Unsigned) offset > ds->ds_size) {
48
	if (offset > ds->ds_size) {
49
		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
49
		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
50
		return (DW_DLV_ERROR);
50
		return (DW_DLV_ERROR);
51
	}
51
	}
52
52
53
	if ((Dwarf_Unsigned) offset == ds->ds_size) {
53
	if (offset == ds->ds_size) {
54
		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
54
		DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
55
		return (DW_DLV_NO_ENTRY);
55
		return (DW_DLV_NO_ENTRY);
56
	}
56
	}
(-)b/contrib/elftoolchain/libdwarf/libdwarf.h (-2 / +2 lines)
Lines 24-30 Link Here
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
25
 * SUCH DAMAGE.
26
 *
26
 *
27
 * $Id: libdwarf.h 3174 2015-03-27 17:13:41Z emaste $
27
 * $Id: libdwarf.h 3295 2016-01-08 22:08:10Z jkoshy $
28
 */
28
 */
29
29
30
#ifndef	_LIBDWARF_H_
30
#ifndef	_LIBDWARF_H_
Lines 33-39 Link Here
33
#include <libelf.h>
33
#include <libelf.h>
34
34
35
typedef int		Dwarf_Bool;
35
typedef int		Dwarf_Bool;
36
typedef off_t		Dwarf_Off;
36
typedef uint64_t	Dwarf_Off;
37
typedef uint64_t	Dwarf_Unsigned;
37
typedef uint64_t	Dwarf_Unsigned;
38
typedef uint16_t	Dwarf_Half;
38
typedef uint16_t	Dwarf_Half;
39
typedef uint8_t		Dwarf_Small;
39
typedef uint8_t		Dwarf_Small;
(-)b/contrib/elftoolchain/libdwarf/libdwarf_rw.c (-1 / +13 lines)
Lines 27-33 Link Here
27
27
28
#include "_libdwarf.h"
28
#include "_libdwarf.h"
29
29
30
ELFTC_VCSID("$Id: libdwarf_rw.c 2952 2013-06-26 19:09:40Z kaiwang27 $");
30
ELFTC_VCSID("$Id: libdwarf_rw.c 3286 2015-12-31 16:45:46Z emaste $");
31
31
32
uint64_t
32
uint64_t
33
_dwarf_read_lsb(uint8_t *data, uint64_t *offsetp, int bytes_to_read)
33
_dwarf_read_lsb(uint8_t *data, uint64_t *offsetp, int bytes_to_read)
Lines 42-51 _dwarf_read_lsb(uint8_t *data, uint64_t *offsetp, int bytes_to_read) Link Here
42
	case 8:
42
	case 8:
43
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
43
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
44
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
44
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
45
		/* FALLTHROUGH */
45
	case 4:
46
	case 4:
46
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
47
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
48
		/* FALLTHROUGH */
47
	case 2:
49
	case 2:
48
		ret |= ((uint64_t) src[1]) << 8;
50
		ret |= ((uint64_t) src[1]) << 8;
51
		/* FALLTHROUGH */
49
	case 1:
52
	case 1:
50
		ret |= src[0];
53
		ret |= src[0];
51
		break;
54
		break;
Lines 71-80 _dwarf_decode_lsb(uint8_t **data, int bytes_to_read) Link Here
71
	case 8:
74
	case 8:
72
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
75
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
73
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
76
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
77
		/* FALLTHROUGH */
74
	case 4:
78
	case 4:
75
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
79
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
80
		/* FALLTHROUGH */
76
	case 2:
81
	case 2:
77
		ret |= ((uint64_t) src[1]) << 8;
82
		ret |= ((uint64_t) src[1]) << 8;
83
		/* FALLTHROUGH */
78
	case 1:
84
	case 1:
79
		ret |= src[0];
85
		ret |= src[0];
80
		break;
86
		break;
Lines 171-181 _dwarf_write_lsb(uint8_t *data, uint64_t *offsetp, uint64_t value, Link Here
171
		dst[6] = (value >> 48) & 0xff;
177
		dst[6] = (value >> 48) & 0xff;
172
		dst[5] = (value >> 40) & 0xff;
178
		dst[5] = (value >> 40) & 0xff;
173
		dst[4] = (value >> 32) & 0xff;
179
		dst[4] = (value >> 32) & 0xff;
180
		/* FALLTHROUGH */
174
	case 4:
181
	case 4:
175
		dst[3] = (value >> 24) & 0xff;
182
		dst[3] = (value >> 24) & 0xff;
176
		dst[2] = (value >> 16) & 0xff;
183
		dst[2] = (value >> 16) & 0xff;
184
		/* FALLTHROUGH */
177
	case 2:
185
	case 2:
178
		dst[1] = (value >> 8) & 0xff;
186
		dst[1] = (value >> 8) & 0xff;
187
		/* FALLTHROUGH */
179
	case 1:
188
	case 1:
180
		dst[0] = value & 0xff;
189
		dst[0] = value & 0xff;
181
		break;
190
		break;
Lines 222-234 _dwarf_write_msb(uint8_t *data, uint64_t *offsetp, uint64_t value, Link Here
222
		dst[5] = (value >> 16) & 0xff;
231
		dst[5] = (value >> 16) & 0xff;
223
		dst[4] = (value >> 24) & 0xff;
232
		dst[4] = (value >> 24) & 0xff;
224
		value >>= 32;
233
		value >>= 32;
234
		/* FALLTHROUGH */
225
	case 4:
235
	case 4:
226
		dst[3] = value & 0xff;
236
		dst[3] = value & 0xff;
227
		dst[2] = (value >> 8) & 0xff;
237
		dst[2] = (value >> 8) & 0xff;
228
		value >>= 16;
238
		value >>= 16;
239
		/* FALLTHROUGH */
229
	case 2:
240
	case 2:
230
		dst[1] = value & 0xff;
241
		dst[1] = value & 0xff;
231
		value >>= 8;
242
		value >>= 8;
243
		/* FALLTHROUGH */
232
	case 1:
244
	case 1:
233
		dst[0] = value & 0xff;
245
		dst[0] = value & 0xff;
234
		break;
246
		break;
(-)b/contrib/elftoolchain/libdwarf/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/libelftc/Makefile (-11 / +2 lines)
Lines 1-4 Link Here
1
# $Id: Makefile 2859 2013-01-05 09:21:54Z jkoshy $
1
# $Id: Makefile 3292 2016-01-06 21:46:32Z jkoshy $
2
2
3
TOP=	${.CURDIR}/..
3
TOP=	${.CURDIR}/..
4
4
Lines 45-60 MLINKS= elftc_bfd_find_target.3 elftc_bfd_target_byteorder.3 \ Link Here
45
	elftc_string_table_create.3 elftc_string_table_destroy.3 \
45
	elftc_string_table_create.3 elftc_string_table_destroy.3 \
46
	elftc_string_table_create.3 elftc_string_table_image.3 \
46
	elftc_string_table_create.3 elftc_string_table_image.3 \
47
	elftc_string_table_create.3 elftc_string_table_insert.3 \
47
	elftc_string_table_create.3 elftc_string_table_insert.3 \
48
	elftc_string_table_create.3 elftc_string_table_lookup.3 \
48
	elftc_string_table_create.3 elftc_string_table_lookup.3
49
	elftc_symbol_table_create.3 elftc_symbol_table_create_nested.3 \
50
	elftc_symbol_table_create.3 elftc_symbol_table_delete_name.3 \
51
	elftc_symbol_table_create.3 elftc_symbol_table_delete_entry.3 \
52
	elftc_symbol_table_create.3 elftc_symbol_table_destroy.3 \
53
	elftc_symbol_table_create.3 elftc_symbol_table_from_section.3 \
54
	elftc_symbol_table_create.3 elftc_symbol_table_insert.3 \
55
	elftc_symbol_table_create.3 elftc_symbol_table_iterate.3 \
56
	elftc_symbol_table_create.3 elftc_symbol_table_lookup.3 \
57
	elftc_symbol_table_create.3 elftc_symbol_table_to_image.3
58
49
59
.if	!make(clean) && !make(clobber)
50
.if	!make(clean) && !make(clobber)
60
.BEGIN:	.SILENT
51
.BEGIN:	.SILENT
(-)b/contrib/elftoolchain/libelftc/elftc_bfd_find_target.3 (-3 / +7 lines)
Lines 21-27 Link Here
21
.\" out of the use of this software, even if advised of the possibility of
21
.\" out of the use of this software, even if advised of the possibility of
22
.\" such damage.
22
.\" such damage.
23
.\"
23
.\"
24
.\" $Id: elftc_bfd_find_target.3 2251 2011-11-30 16:50:06Z jkoshy $
24
.\" $Id: elftc_bfd_find_target.3 3348 2016-01-18 14:18:50Z emaste $
25
.\"
25
.\"
26
.Dd November 30, 2011
26
.Dd November 30, 2011
27
.Os
27
.Os
Lines 58-66 Binary object descriptors encapsulate properties of an object format Link Here
58
such as its file representation, ELF class, and byte endianness.
58
such as its file representation, ELF class, and byte endianness.
59
.Pp
59
.Pp
60
Known descriptor names and their properties include:
60
Known descriptor names and their properties include:
61
.Bl -column -offset "XXXX" ".Li elf32-x86-64-freebsd" "Object format" "Byte Order" "ELF Class"
61
.Bl -column -offset "XXXX" ".Li elf32-x86-64-freebsd" "Object format" "Byte Order" "Bit Width"
62
.It Em Name Ta Em "Object Format" Ta Em "Byte Order" Ta Em "ELF Class"
62
.It Em Name Ta Em "Object Format" Ta Em "Byte Order" Ta Em "Bit Width"
63
.It Li binary Ta Binary Ta - Ta -
63
.It Li binary Ta Binary Ta - Ta -
64
.It Li efi-app-ia32 Ta PE Ta LSB Ta 32
65
.It Li efi-app-x86_64 Ta PE Ta LSB Ta 64
64
.It Li elf32-avr Ta ELF Ta LSB Ta 32
66
.It Li elf32-avr Ta ELF Ta LSB Ta 32
65
.It Li elf32-big Ta ELF Ta MSB Ta 32
67
.It Li elf32-big Ta ELF Ta MSB Ta 32
66
.It Li elf32-bigarm Ta ELF Ta MSB Ta 32
68
.It Li elf32-bigarm Ta ELF Ta MSB Ta 32
Lines 101-106 Known descriptor names and their properties include: Link Here
101
.It Li elf64-x86-64 Ta ELF Ta LSB Ta 64
103
.It Li elf64-x86-64 Ta ELF Ta LSB Ta 64
102
.It Li elf64-x86-64-freebsd Ta ELF Ta LSB Ta 64
104
.It Li elf64-x86-64-freebsd Ta ELF Ta LSB Ta 64
103
.It Li ihex Ta IHEX Ta - Ta -
105
.It Li ihex Ta IHEX Ta - Ta -
106
.It Li pei-i386 Ta PE Ta LSB Ta 32
107
.It Li pei-x86-64 Ta PE Ta LSB Ta 64
104
.It Li srec Ta SREC Ta - Ta -
108
.It Li srec Ta SREC Ta - Ta -
105
.It Li symbolsrec Ta SREC Ta - Ta -
109
.It Li symbolsrec Ta SREC Ta - Ta -
106
.El
110
.El
(-)b/contrib/elftoolchain/libelftc/elftc_copyfile.c (-10 / +14 lines)
Lines 37-43 Link Here
37
#include <sys/mman.h>
37
#include <sys/mman.h>
38
#endif
38
#endif
39
39
40
ELFTC_VCSID("$Id: elftc_copyfile.c 2981 2014-02-01 02:41:13Z jkoshy $");
40
ELFTC_VCSID("$Id: elftc_copyfile.c 3297 2016-01-09 15:26:34Z jkoshy $");
41
41
42
/*
42
/*
43
 * Copy the contents referenced by 'ifd' to 'ofd'.  Returns 0 on
43
 * Copy the contents referenced by 'ifd' to 'ofd'.  Returns 0 on
Lines 47-57 ELFTC_VCSID("$Id: elftc_copyfile.c 2981 2014-02-01 02:41:13Z jkoshy $"); Link Here
47
int
47
int
48
elftc_copyfile(int ifd, int ofd)
48
elftc_copyfile(int ifd, int ofd)
49
{
49
{
50
	size_t file_size, n;
50
	int buf_mmapped;
51
	int buf_mmapped;
51
	struct stat sb;
52
	struct stat sb;
52
	char *b, *buf;
53
	char *b, *buf;
53
	ssize_t nw;
54
	ssize_t nr, nw;
54
	size_t n;
55
55
56
	/* Determine the input file's size. */
56
	/* Determine the input file's size. */
57
	if (fstat(ifd, &sb) < 0)
57
	if (fstat(ifd, &sb) < 0)
Lines 63-74 elftc_copyfile(int ifd, int ofd) Link Here
63
63
64
	buf = NULL;
64
	buf = NULL;
65
	buf_mmapped = 0;
65
	buf_mmapped = 0;
66
	file_size = (size_t) sb.st_size;
66
67
67
#if	ELFTC_HAVE_MMAP
68
#if	ELFTC_HAVE_MMAP
68
	/*
69
	/*
69
	 * Prefer mmap() if it is available.
70
	 * Prefer mmap() if it is available.
70
	 */
71
	 */
71
	buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd, (off_t) 0);
72
	buf = mmap(NULL, file_size, PROT_READ, MAP_SHARED, ifd, (off_t) 0);
72
	if (buf != MAP_FAILED)
73
	if (buf != MAP_FAILED)
73
		buf_mmapped = 1;
74
		buf_mmapped = 1;
74
	else
75
	else
Lines 80-103 elftc_copyfile(int ifd, int ofd) Link Here
80
	 * failed, allocate a buffer, and read in input data.
81
	 * failed, allocate a buffer, and read in input data.
81
	 */
82
	 */
82
	if (buf_mmapped == false) {
83
	if (buf_mmapped == false) {
83
		if ((buf = malloc(sb.st_size)) == NULL)
84
		if ((buf = malloc(file_size)) == NULL)
84
			return (-1);
85
		if (read(ifd, buf, sb.st_size) != sb.st_size) {
86
			free(buf);
87
			return (-1);
85
			return (-1);
86
		b = buf;
87
		for (n = file_size; n > 0; n -= (size_t) nr, b += nr) {
88
			if ((nr = read(ifd, b, n)) < 0) {
89
				free(buf);
90
				return (-1);
91
			}
88
		}
92
		}
89
	}
93
	}
90
94
91
	/*
95
	/*
92
	 * Write data to the output file descriptor.
96
	 * Write data to the output file descriptor.
93
	 */
97
	 */
94
	for (n = sb.st_size, b = buf; n > 0; n -= nw, b += nw)
98
	for (n = file_size, b = buf; n > 0; n -= (size_t) nw, b += nw)
95
		if ((nw = write(ofd, b, n)) <= 0)
99
		if ((nw = write(ofd, b, n)) <= 0)
96
			break;
100
			break;
97
101
98
	/* Release the input buffer. */
102
	/* Release the input buffer. */
99
#if	ELFTC_HAVE_MMAP
103
#if	ELFTC_HAVE_MMAP
100
	if (buf_mmapped && munmap(buf, sb.st_size) < 0)
104
	if (buf_mmapped && munmap(buf, file_size) < 0)
101
		return (-1);
105
		return (-1);
102
#endif
106
#endif
103
107
(-)b/contrib/elftoolchain/libelftc/elftc_demangle.c (-5 / +5 lines)
Lines 33-42 Link Here
33
33
34
#include "_libelftc.h"
34
#include "_libelftc.h"
35
35
36
ELFTC_VCSID("$Id: elftc_demangle.c 3174 2015-03-27 17:13:41Z emaste $");
36
ELFTC_VCSID("$Id: elftc_demangle.c 3296 2016-01-09 14:17:28Z jkoshy $");
37
37
38
static int
38
static unsigned int
39
is_mangled(const char *s, int style)
39
is_mangled(const char *s, unsigned int style)
40
{
40
{
41
41
42
	switch (style) {
42
	switch (style) {
Lines 58-64 is_mangled(const char *s, int style) Link Here
58
}
58
}
59
59
60
static char *
60
static char *
61
demangle(const char *s, int style, int rc)
61
demangle(const char *s, unsigned int style, unsigned int rc)
62
{
62
{
63
63
64
	(void) rc;			/* XXX */
64
	(void) rc;			/* XXX */
Lines 76-82 int Link Here
76
elftc_demangle(const char *mangledname, char *buffer, size_t bufsize,
76
elftc_demangle(const char *mangledname, char *buffer, size_t bufsize,
77
    unsigned int flags)
77
    unsigned int flags)
78
{
78
{
79
	int style, rc;
79
	unsigned int style, rc;
80
	char *rlt;
80
	char *rlt;
81
81
82
	style = flags & 0xFFFF;
82
	style = flags & 0xFFFF;
(-)a/contrib/elftoolchain/libelftc/elftc_symbol_table_create.3 (-529 lines)
Removed Link Here
1
.\" Copyright (c) 2012 Joseph Koshy.  All rights reserved.
2
.\"
3
.\" Redistribution and use in source and binary forms, with or without
4
.\" modification, are permitted provided that the following conditions
5
.\" are met:
6
.\" 1. Redistributions of source code must retain the above copyright
7
.\"    notice, this list of conditions and the following disclaimer.
8
.\" 2. Redistributions in binary form must reproduce the above copyright
9
.\"    notice, this list of conditions and the following disclaimer in the
10
.\"    documentation and/or other materials provided with the distribution.
11
.\"
12
.\" This software is provided by Joseph Koshy ``as is'' and
13
.\" any express or implied warranties, including, but not limited to, the
14
.\" implied warranties of merchantability and fitness for a particular purpose
15
.\" are disclaimed.  in no event shall Joseph Koshy be liable
16
.\" for any direct, indirect, incidental, special, exemplary, or consequential
17
.\" damages (including, but not limited to, procurement of substitute goods
18
.\" or services; loss of use, data, or profits; or business interruption)
19
.\" however caused and on any theory of liability, whether in contract, strict
20
.\" liability, or tort (including negligence or otherwise) arising in any way
21
.\" out of the use of this software, even if advised of the possibility of
22
.\" such damage.
23
.\"
24
.\" $Id: elftc_symbol_table_create.3 3182 2015-04-10 16:08:10Z emaste $
25
.\"
26
.Dd December 29, 2012
27
.Os
28
.Dt ELFTC_SYMBOL_TABLE_CREATE 3
29
.Sh NAME
30
.Nm elftc_elf_symbol_table_from_section ,
31
.Nm elftc_symbol_table_count ,
32
.Nm elftc_symbol_table_create ,
33
.Nm elftc_symbol_table_create_nested ,
34
.Nm elftc_symbol_table_delete_name ,
35
.Nm elftc_symbol_table_delete_entry ,
36
.Nm elftc_symbol_table_destroy ,
37
.Nm elftc_symbol_table_insert ,
38
.Nm elftc_symbol_table_iterate ,
39
.Nm elftc_symbol_table_lookup ,
40
.Nm elftc_symbol_table_lookup_value ,
41
.Nm elftc_symbol_table_replace ,
42
.Nm elftc_symbol_table_sort ,
43
.Nm elftc_symbol_table_step
44
.Nd symbol table management routines
45
.Sh SYNOPSIS
46
.In libelftc.h
47
.Bd -literal
48
typedef struct _Elftc_Symbol_Table Elftc_Symbol_Table;
49
50
typedef struct _Elftc_Symbol {
51
	... library private fields ...
52
	const char	*sym_name;
53
	uintptr_t	sym_value;
54
} Elftc_Symbol;
55
.Ed
56
.Ft size_t
57
.Fn elftc_symbol_table_count "Elftc_Symbol_Table *table"
58
.Ft "Elftc_Symbol_Table *"
59
.Fo elftc_symbol_table_create
60
.Fa "size_t entrysize"
61
.Fa "int sizehint"
62
.Fc
63
.Ft "Elftc_Symbol_Table *"
64
.Fo elftc_symbol_table_create_nested
65
.Fa "Elftc_Symbol_Table *table"
66
.Fa "int sizehint"
67
.Fc
68
.Ft int
69
.Fo elftc_symbol_table_delete_name
70
.Fa "Elftc_Symbol_Table *table"
71
.Fa "const char *name"
72
.Fc
73
.Ft int
74
.Fo elftc_symbol_table_delete_entry
75
.Fa "Elftc_Symbol_Table *table"
76
.Fa "Elftc_Symbol *entry"
77
.Fc
78
.Ft int
79
.Fn elftc_symbol_table_destroy "Elftc_Symbol_Table *table"
80
.Ft "Elftc_Symbol *entry"
81
.Fo elftc_symbol_table_insert
82
.Fa "Elftc_Symbol_Table *table"
83
.Fa "const char *symbolname"
84
.Fa "int *status"
85
.Fc
86
.Ft int
87
.Fo elftc_symbol_table_iterate
88
.Fa "Elftc_Symbol_Table *table"
89
.Fa "int (*iterfn)(Elftc_Symbol *entry, void *cookie)"
90
.Fa "void *cookie"
91
.Fc
92
.Ft "Elftc_Symbol *"
93
.Fo elftc_symbol_table_lookup
94
.Fa "Elftc_Symbol_Table *table"
95
.Fa "const char *symbolname"
96
.Fc
97
.Ft "Elftc_Elf_Symbol *"
98
.Fo elftc_symbol_table_lookup_value
99
.Fa "Elftc_Symbol_Table *table"
100
.Fa "uintptr_t value"
101
.Fa "int searchflags"
102
.Fc
103
.Ft int
104
.Fo elftc_symbol_table_replace
105
.Fa "Elftc_Symbol_Table *table"
106
.Fa "Elftc_Symbol *sym1"
107
.Fa "Elftc_Symbol *sym2"
108
.Fc
109
.Ft int
110
.Fo elftc_symbol_table_sort
111
.Fa "Elftc_Symbol_Table *table"
112
.Fa "int (*cmpfn)(Elftc_Symbol *s1, Elftc_Symbol *s2)"
113
.Fc
114
.Ft "Elftc_Symbol *"
115
.Fo elftc_symbol_table_step
116
.Fa "Elftc_Symbol_Table *table"
117
.Fa "Elftc_Symbol *cursym"
118
.Fa "int direction"
119
.Fc
120
.Bd -literal
121
typedef struct _Elftc_Elf_Symbol {
122
	... library private fields ...
123
	const char	*sym_name;
124
	Gelf_Sym	sym_elf;
125
} Elftc_Elf_Symbol;
126
.Ed
127
.Ft "Elftc_Symbol_Table *"
128
.Fo elftc_elf_symbol_table_from_section
129
.Fa "Elf_Scn *symscn"
130
.Fa "Elf_Scn *strscn"
131
.Fc
132
.Sh DESCRIPTION
133
This manual page documents convenience routines for handling symbol
134
tables.
135
Two flavors of symbol tables are supported:
136
.Bl -bullet -compact
137
.It
138
.Dq Regular
139
symbol tables supporting insertion, deletion and lookup of entries by
140
name or by value, sorting of entries, and stepping through entries in
141
the table's current traversal order.
142
.It
143
.Dq ELF-centric
144
symbol tables support additional operations for conversions to and
145
from the symbol table format understood by
146
.Lb libelf .
147
.El
148
The default traversal order for a symbol table is the order in which
149
entries were inserted into it.
150
This traversal order may be changed using function
151
.Fn elftc_symbol_table_sort .
152
.Ss Operations on Regular Symbol Tables
153
Regular symbol tables use symbols that are subtypes of
154
.Vt Elftc_Symbol ,
155
as described in the section
156
.Sx "Structure of a Symbol Table Entry"
157
below.
158
.Pp
159
Function
160
.Fn elftc_symbol_table_count
161
returns the number of entries currently in the symbol table.
162
.Pp
163
Function
164
.Fn elftc_symbol_table_create
165
creates a new, empty symbol table.
166
The argument
167
.Ar entrysize
168
specifies the size of each symbol table entry, as described
169
in the section
170
.Sx "Structure of a Symbol Table Entry"
171
below.
172
The argument
173
.Ar sizehint
174
specifies the expected number of symbol table entries.
175
If
176
.Ar sizehint
177
is zero, an implementation-defined default will be used.
178
.Pp
179
Function
180
.Fn elftc_symbol_table_create_nested
181
creates a symbol table whose search scope nests inside that of a
182
parent symbol table.
183
The argument
184
.Ar parent
185
specifies the parent symbol table to nest under.
186
The argument
187
.Ar sizehint
188
specifies the expected number of symbol table entries.
189
If
190
.Ar sizehint
191
is zero, an implementation-defined default will be used instead.
192
.Pp
193
The function
194
.Fn elftc_symbol_table_delete_name
195
removes the symbol entry named by the argument
196
.Ar name
197
from the symbol table specified by argument
198
.Ar table ,
199
according to the rules described in section
200
.Sx "Symbol Search Rules" .
201
.Pp
202
The function
203
.Fn elftc_symbol_table_delete_entry
204
removes the symbol table entry specified by argument
205
.Ar entry
206
from the symbol table specified by argument
207
.Ar table .
208
.Pp
209
Function
210
.Fn elftc_symbol_table_destroy
211
is used to destroy a symbol table and free up its internal
212
resources.
213
.Pp
214
The function
215
.Fn elftc_symbol_table_insert
216
inserts a symbol entry for the name specified by argument
217
.Ar symbolname
218
into the symbol table specified by argument
219
.Ar table ,
220
returning a pointer to a symbol table entry.
221
The argument
222
.Ar status
223
should point to a location that will be updated with one of
224
the following values:
225
.Bl -tag -width indent -compact -offset indent
226
.It Dv ELFTC_INSERT_ERROR
227
An error occurred during insertion of the symbol.
228
.It Dv ELFTC_INSERT_EXISTING
229
The name in argument
230
.Ar symbolname
231
was already in the symbol table, and a pointer to the existing
232
symbol table entry is being returned.
233
.It Dv ELFTC_INSERT_NEW
234
A new symbol table entry was allocated for the symbol name
235
in
236
.Ar symbolname .
237
The application will need to initialize the application-specific
238
fields of the symbol table entry.
239
.El
240
Insertion obeys the scoping rules described in section
241
.Sx "Symbol Search Rules" .
242
.Pp
243
The function
244
.Fn elftc_symbol_table_iterate
245
iterates over the symbol table specifed by argument
246
.Ar table ,
247
applying the function pointed to by argument
248
.Ar iterfn
249
to each symbol table entry.
250
The return value from the function
251
.Ar iterfn
252
controls progress of the iteration:
253
.Bl -tag -width indent -compact -offset indent
254
.It Dv ELFTC_ITERATE_ABORT
255
Terminates the iteration.
256
.It Dv ELFTC_ITERATE_CONTINUE
257
Iteration will continue on to the next element in the symbol table.
258
.El
259
Argument
260
.Ar cookie
261
will be passed to each invocation of
262
.Ar iterfn ,
263
and may be used to track persistent state.
264
The ordering of symbol table entries presented to function
265
.Ar iterfn
266
is not defined.
267
The behavior of the iteration is undefined if
268
.Ar iterfn
269
adds or deletes symbol entries from a symbol table that currently
270
being iterated through.
271
.Pp
272
Function
273
.Fn elftc_symbol_table_lookup
274
returns the symbol entry corresponding to the name of the symbol
275
in argument
276
.Ar symbolname .
277
.Pp
278
Function
279
.Fn elftc_symbol_table_lookup_value
280
returns the symbol entry that has a
281
.Va sym_value
282
field that is closest to the value specified in argument
283
.Ar value .
284
The argument
285
.Ar table
286
should point to a symbol table, that has been sorted
287
by a prior call to
288
.Fn elftc_symbol_table_sort .
289
The argument
290
.Ar searchflags
291
can be a combination of the following flags:
292
.Bl -tag -width indent -compact -offset indent
293
.It Dv ELFTC_SEARCH_FORWARD
294
Find the symbol entry with the next higher value in its
295
.Va sym_value
296
field.
297
.It Dv ELFTC_SEARCH_BACKWARD
298
Find the symbol entry with next lower value in its
299
.Va sym_value
300
field.
301
.El
302
If both
303
.Dv ELFTC_SEARCH_FORWARD
304
and
305
.Dv ELFTC_SEARCH_BACKWARD
306
are specified, then this function will return the symbol that is
307
closest to the argument
308
.Ar value .
309
.Pp
310
Function
311
.Fn elftc_symbol_table_replace
312
moves the symbol table entry pointed to by argument
313
.Ar sym2
314
into the traversal position for the entry pointed to by
315
.Ar sym1 ,
316
and implicitly deletes the entry pointed to by argument
317
.Ar sym1 .
318
Argument
319
.Ar table
320
should point to a valid symbol table.
321
.Pp
322
Function
323
.Fn elftc_symbol_table_sort
324
is used to define an ordering of symbol entries in a symbol
325
table.
326
This ordering will be associated with the symbol table till the next
327
call to function
328
.Fn elftc_symbol_table_insert ,
329
.Fn elftc_symbol_table_delete_name
330
or
331
.Fn elftc_symbol_table_delete_entry .
332
The argument
333
.Ar cmpfn
334
should point to a function that compares two symbol entries pointed
335
to by
336
.Ar s1
337
and
338
.Ar s2
339
and returns -1, 0, or 1, depending whether
340
.Ar s1
341
is less, equal to, or greater than
342
.Ar s2
343
respectively.
344
.Pp
345
Function
346
.Fn elftc_symbol_table_step
347
is used to step to the next symbol in a sorted symbol table.
348
Argument
349
.Ar table
350
should point to a symbol table.
351
The argument
352
.Ar cursym
353
specifies the current symbol.
354
The argument
355
.Ar direction
356
specifies the direction to step:
357
.Bl -tag -width indent -compact -offset ident
358
.It Dv ELFTC_STEP_NEXT
359
Return the symbol which follows the argument
360
.Ar cursym
361
in the current traversal order.
362
If argument
363
.Ar cursym
364
is NULL, return the first symbol in the current
365
traversal order.
366
.It Dv ELFTC_STEP_PREVIOUS
367
Return the symbol which precedes the argument
368
.Ar cursym
369
in the current traversal order.
370
If argument
371
.Ar cursym
372
is NULL, return the last symbol in the current
373
traversal order.
374
.El
375
.Ss Operations on ELF-centric symbol tables
376
ELF-centric symbol tables use symbols that are subtypes of
377
.Vt Elftc_Elf_Symbol ,
378
as described in the section
379
.Sx "Structure of a Symbol Table Entry"
380
below.
381
.Pp
382
In addition to the operations on regular symbol tables listed above,
383
these symbol tables may be used with the following additional
384
functions.
385
.Pp
386
The function
387
.Fn elftc_elf_symbol_table_from_section
388
builds a symbol table from the contents of an ELF section.
389
The argument
390
.Ar symscn
391
should reference an ELF section of type
392
.Dv SHT_SYMTAB
393
or
394
.Dv SHT_DYNSYM .
395
The argument
396
.Ar strscn
397
should reference an ELF section of type
398
.Dv SHT_STRTAB
399
containing the string table associated wit section
400
.Ar symscn .
401
.Ss Structure of a Symbol Table Entry
402
The symbol tables managed by
403
.Lb libelftc
404
are collections of symbol table entries.
405
Each entry should be a subtype of one of the
406
.Vt Elftc_Symbol
407
or
408
.Vt Elftc_Elf_Symbol
409
types.
410
In other words, each entry should have an
411
.Vt Elftc_Symbol
412
or
413
.Vt Elftc_Elf_Symbol
414
structure as its first member, before any application specific
415
fields.
416
For example:
417
.Bd -literal -offset indent
418
struct _MySymbol {
419
	Elftc_Symbol	sym_base;
420
	... other application-specific fields ...
421
};
422
.Ed
423
.Pp
424
The size of the combined entry is indicated to the library
425
at the time of creating a new symbol table.
426
Applications may then cast the returned pointers from these
427
routines to the appropriate type:
428
.Bd -literal -offset indent
429
struct _MySymbol *mysym;
430
431
mysym = (struct _MySymbol *) elftc_symbol_table_lookup(table,
432
	    name);
433
.Ed
434
.Pp
435
The
436
.Vt Elftc_Symbol
437
type has two public fields:
438
.Bl -tag -width ".Va sym_value" -compact -offset indent
439
.It Va sym_name
440
Points to a NUL-terminated string containing the symbol's name.
441
The application should not change the value of this field.
442
.It Va sym_value
443
The value associated with this symbol.
444
This field is entirely under the application's control.
445
.El
446
.Pp
447
The
448
.Vt Elftc_Elf_Symbol
449
type has two public fields:
450
.Bl -tag -width ".Va sym_value" -compact -offset indent
451
.It Va sym_name
452
Points to a NUL-terminated string containing the symbol's name.
453
The application should not change the value of this field.
454
.It Va sym_elf
455
A structure of type
456
.Vt Gelf_Sym
457
containing ELF symbol information.
458
This field is entirely under the application's control.
459
.El
460
.Ss Symbol Search Rules
461
During lookups, symbols are looked up first in the symbol table passed in
462
to the
463
.Fn elftc_symbol_table_lookup
464
function.
465
If the specified symbol is not found, and if the symbol table has a
466
parent, then the search continues recursively up the chain of parent
467
symbol tables till either a matching symbol is found or till there are
468
no more parent symbol tables to search in.
469
.Pp
470
Insertions and deletion only work on the specified symbol table and
471
do not recurse into parent symbol tables.
472
.Ss Memory Management
473
The
474
.Lb libelftc
475
manages its memory allocations.
476
Applications should not free the pointers returned by the
477
API documented in this manual page.
478
.Sh RETURN VALUES
479
Function
480
.Fn elftc_symbol_table_count
481
returns a count of the number of symbol table entries as an unsigned
482
value.
483
.Pp
484
Functions
485
.Fn elftc_symbol_table_create ,
486
.Fn elftc_symbol_table_create_nested
487
and
488
.Fn elftc_symbol_table_from_section
489
return a pointer to an opaque structure of type
490
.Vt Elftc_Symbol_Table
491
on success, or return NULL in case of an error.
492
.Pp
493
Functions
494
.Fn elftc_symbol_table_delete_name ,
495
.Fn elftc_symbol_table_delete_name
496
.Fn elftc_symbol_table_destroy ,
497
.Fn elftc_symbol_table_replace
498
and
499
.Fn elftc_symbol_table_sort
500
return a non-zero value on success, or return zero in case of an error.
501
.Pp
502
Functions
503
.Fn elftc_symbol_table_insert ,
504
.Fn elftc_symbol_table_lookup
505
and
506
.Fn elftc_symbol_table_lookup_value
507
return a pointer to a structure that is a subtype of
508
.Vt Elftc_Symbol
509
on success, or return NULL in case of an error.
510
.Pp
511
The function
512
.Fn elftc_symbol_table_step
513
return a pointer to a structure that is a subtype of
514
.Vt Elftc_Symbol
515
on success.
516
The function returns NULL if there are no more elements in the
517
specified traversal direction.
518
.Pp
519
The function
520
.Fn elftc_symbol_table_iterate
521
returns
522
.Dv ELFTC_ITERATE_SUCCESS
523
if the symbol table was successfully traversed, or
524
.Dv ELFTC_ITERATE_ABORT
525
in case the iteration function aborted the traversal.
526
.Sh SEE ALSO
527
.Xr dwarf 3 ,
528
.Xr elf 3 ,
529
.Xr elftc 3
(-)b/contrib/elftoolchain/libelftc/libelftc.h (-2 / +4 lines)
Lines 24-30 Link Here
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 *
25
 *
26
 * $FreeBSD: users/kaiwang27/elftc/libelftc.h 392 2009-05-31 19:17:46Z kaiwang27 $
26
 * $FreeBSD: users/kaiwang27/elftc/libelftc.h 392 2009-05-31 19:17:46Z kaiwang27 $
27
 * $Id: libelftc.h 3174 2015-03-27 17:13:41Z emaste $
27
 * $Id: libelftc.h 3309 2016-01-10 09:10:51Z kaiwang27 $
28
 */
28
 */
29
29
30
#ifndef	_LIBELFTC_H_
30
#ifndef	_LIBELFTC_H_
Lines 46-52 typedef enum { Link Here
46
	ETF_ELF,
46
	ETF_ELF,
47
	ETF_BINARY,
47
	ETF_BINARY,
48
	ETF_SREC,
48
	ETF_SREC,
49
	ETF_IHEX
49
	ETF_IHEX,
50
	ETF_PE,
51
	ETF_EFI,
50
} Elftc_Bfd_Target_Flavor;
52
} Elftc_Bfd_Target_Flavor;
51
53
52
/*
54
/*
(-)b/contrib/elftoolchain/libelftc/libelftc_bfdtarget.c (-1 / +25 lines)
Lines 30-36 Link Here
30
30
31
#include "_libelftc.h"
31
#include "_libelftc.h"
32
32
33
ELFTC_VCSID("$Id: libelftc_bfdtarget.c 3174 2015-03-27 17:13:41Z emaste $");
33
ELFTC_VCSID("$Id: libelftc_bfdtarget.c 3309 2016-01-10 09:10:51Z kaiwang27 $");
34
34
35
struct _Elftc_Bfd_Target _libelftc_targets[] = {
35
struct _Elftc_Bfd_Target _libelftc_targets[] = {
36
36
Lines 375-380 struct _Elftc_Bfd_Target _libelftc_targets[] = { Link Here
375
	},
375
	},
376
376
377
	{
377
	{
378
		.bt_name    = "efi-app-ia32",
379
		.bt_type    = ETF_EFI,
380
		.bt_machine = EM_386,
381
	},
382
383
	{
384
		.bt_name    = "efi-app-x86_64",
385
		.bt_type    = ETF_EFI,
386
		.bt_machine = EM_X86_64,
387
	},
388
389
	{
390
		.bt_name    = "pei-i386",
391
		.bt_type    = ETF_PE,
392
		.bt_machine = EM_386,
393
	},
394
395
	{
396
		.bt_name    = "pei-x86-64",
397
		.bt_type    = ETF_PE,
398
		.bt_machine = EM_X86_64,
399
	},
400
401
	{
378
		.bt_name = NULL,
402
		.bt_name = NULL,
379
		.bt_type = ETF_NONE,
403
		.bt_type = ETF_NONE,
380
	},
404
	},
(-)b/contrib/elftoolchain/libelftc/libelftc_dem_gnu3.c (-10 / +12 lines)
Lines 36-42 Link Here
36
36
37
#include "_libelftc.h"
37
#include "_libelftc.h"
38
38
39
ELFTC_VCSID("$Id: libelftc_dem_gnu3.c 3212 2015-05-17 13:40:55Z kaiwang27 $");
39
ELFTC_VCSID("$Id: libelftc_dem_gnu3.c 3291 2016-01-04 02:36:38Z emaste $");
40
40
41
/**
41
/**
42
 * @file cpp_demangle.c
42
 * @file cpp_demangle.c
Lines 1262-1272 cpp_demangle_read_encoding(struct cpp_demangle_data *ddata) Link Here
1262
			if (!cpp_demangle_push_str(ddata,
1262
			if (!cpp_demangle_push_str(ddata,
1263
			    "non-transaction clone for ", 26))
1263
			    "non-transaction clone for ", 26))
1264
				return (0);
1264
				return (0);
1265
			break;
1265
		case 't':
1266
		case 't':
1266
		default:
1267
		default:
1267
			if (!cpp_demangle_push_str(ddata,
1268
			if (!cpp_demangle_push_str(ddata,
1268
			    "transaction clone for ", 22))
1269
			    "transaction clone for ", 22))
1269
				return (0);
1270
				return (0);
1271
			break;
1270
		}
1272
		}
1271
		++ddata->cur;
1273
		++ddata->cur;
1272
		return (cpp_demangle_read_encoding(ddata));
1274
		return (cpp_demangle_read_encoding(ddata));
Lines 1895-1929 cpp_demangle_read_subst(struct cpp_demangle_data *ddata) Link Here
1895
1897
1896
	case SIMPLE_HASH('S', 'd'):
1898
	case SIMPLE_HASH('S', 'd'):
1897
		/* std::basic_iostream<char, std::char_traits<char> > */
1899
		/* std::basic_iostream<char, std::char_traits<char> > */
1898
		if (!cpp_demangle_push_str(ddata, "std::iostream", 19))
1900
		if (!cpp_demangle_push_str(ddata, "std::basic_iostream", 19))
1899
			return (0);
1901
			return (0);
1900
		ddata->last_sname = "iostream";
1902
		ddata->last_sname = "basic_iostream";
1901
		ddata->cur += 2;
1903
		ddata->cur += 2;
1902
		if (*ddata->cur == 'I')
1904
		if (*ddata->cur == 'I')
1903
			return (cpp_demangle_read_subst_stdtmpl(ddata,
1905
			return (cpp_demangle_read_subst_stdtmpl(ddata,
1904
			    "std::iostream", 19));
1906
			    "std::basic_iostream", 19));
1905
		return (1);
1907
		return (1);
1906
1908
1907
	case SIMPLE_HASH('S', 'i'):
1909
	case SIMPLE_HASH('S', 'i'):
1908
		/* std::basic_istream<char, std::char_traits<char> > */
1910
		/* std::basic_istream<char, std::char_traits<char> > */
1909
		if (!cpp_demangle_push_str(ddata, "std::istream", 18))
1911
		if (!cpp_demangle_push_str(ddata, "std::basic_istream", 18))
1910
			return (0);
1912
			return (0);
1911
		ddata->last_sname = "istream";
1913
		ddata->last_sname = "basic_istream";
1912
		ddata->cur += 2;
1914
		ddata->cur += 2;
1913
		if (*ddata->cur == 'I')
1915
		if (*ddata->cur == 'I')
1914
			return (cpp_demangle_read_subst_stdtmpl(ddata,
1916
			return (cpp_demangle_read_subst_stdtmpl(ddata,
1915
			    "std::istream", 18));
1917
			    "std::basic_istream", 18));
1916
		return (1);
1918
		return (1);
1917
1919
1918
	case SIMPLE_HASH('S', 'o'):
1920
	case SIMPLE_HASH('S', 'o'):
1919
		/* std::basic_ostream<char, std::char_traits<char> > */
1921
		/* std::basic_ostream<char, std::char_traits<char> > */
1920
		if (!cpp_demangle_push_str(ddata, "std::ostream", 18))
1922
		if (!cpp_demangle_push_str(ddata, "std::basic_ostream", 18))
1921
			return (0);
1923
			return (0);
1922
		ddata->last_sname = "istream";
1924
		ddata->last_sname = "basic_ostream";
1923
		ddata->cur += 2;
1925
		ddata->cur += 2;
1924
		if (*ddata->cur == 'I')
1926
		if (*ddata->cur == 'I')
1925
			return (cpp_demangle_read_subst_stdtmpl(ddata,
1927
			return (cpp_demangle_read_subst_stdtmpl(ddata,
1926
			    "std::ostream", 18));
1928
			    "std::basic_ostream", 18));
1927
		return (1);
1929
		return (1);
1928
1930
1929
	case SIMPLE_HASH('S', 's'):
1931
	case SIMPLE_HASH('S', 's'):
(-)b/contrib/elftoolchain/libelftc/make-toolchain-version (-2 / +2 lines)
Lines 3-9 Link Here
3
# This script generates a project-wide version identifier for use by
3
# This script generates a project-wide version identifier for use by
4
# the `elftc_version()' API.
4
# the `elftc_version()' API.
5
#
5
#
6
# $Id: make-toolchain-version 2583 2012-09-14 09:49:25Z jkoshy $
6
# $Id: make-toolchain-version 3299 2016-01-09 19:58:46Z jkoshy $
7
7
8
#
8
#
9
# Defaults.
9
# Defaults.
Lines 64-70 done Link Here
64
curdir=`pwd`
64
curdir=`pwd`
65
cd ${top} || usage "ERROR: Cannot change directory to \"${top}\"."
65
cd ${top} || usage "ERROR: Cannot change directory to \"${top}\"."
66
66
67
if [ -d .svn ]; then		# FreeBSD and SF.Net sources.
67
if [ -d .svn -o -d ../.svn ]; then	# FreeBSD and SF.Net sources.
68
    versionstring=" svn:"$(svnversion)
68
    versionstring=" svn:"$(svnversion)
69
elif [ -d CVS ]; then		# NetBSD.
69
elif [ -d CVS ]; then		# NetBSD.
70
    versionstring=" cvs:unknown"
70
    versionstring=" cvs:unknown"
(-)b/contrib/elftoolchain/libelftc/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/libpe/Makefile (+32 lines)
Added Link Here
1
# $Id: Makefile 3349 2016-01-18 21:09:16Z jkoshy $
2
3
TOP=	${.CURDIR}/..
4
5
LIB=	pe
6
7
SRCS=	libpe_buffer.c	\
8
	libpe_coff.c	\
9
	libpe_dos.c	\
10
	libpe_init.c	\
11
	libpe_rich.c	\
12
	libpe_section.c	\
13
	libpe_utils.c	\
14
	pe_buffer.c	\
15
	pe_cntl.c	\
16
	pe_coff.c	\
17
	pe_dos.c	\
18
	pe_flag.c	\
19
	pe_init.c	\
20
	pe_rich.c	\
21
	pe_section.c	\
22
	pe_symtab.c	\
23
	pe_update.c
24
25
INCS=		libpe.h pe.h
26
INCSDIR=	/usr/include
27
28
SHLIB_MAJOR=	1
29
30
WARNS?=	6
31
32
.include "${TOP}/mk/elftoolchain.lib.mk"
(-)b/contrib/elftoolchain/libpe/_libpe.h (+213 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 *
26
 * $Id: _libpe.h 3312 2016-01-10 09:23:51Z kaiwang27 $
27
 */
28
29
#ifndef	__LIBPE_H_
30
#define	__LIBPE_H_
31
32
#include <sys/types.h>
33
#include <sys/queue.h>
34
35
#include "libpe.h"
36
37
#include "_elftc.h"
38
39
typedef struct _PE_SecBuf {
40
	PE_Buffer	 sb_pb;		/* application buffer */
41
	PE_Scn		*sb_ps;		/* PE_Scn pointer */
42
	unsigned int	 sb_flags;	/* buffer flags */
43
	STAILQ_ENTRY(_PE_SecBuf) sb_next;
44
} PE_SecBuf;
45
46
struct _PE_Scn {
47
	PE		*ps_pe;		/* PE descriptor */
48
	PE_SecHdr	 ps_sh;		/* section header */
49
	unsigned int	 ps_ndx;	/* 1-based section index */
50
	unsigned int	 ps_flags;	/* section flags */
51
	unsigned int	 ps_falign;	/* section file alignment */
52
	STAILQ_HEAD(, _PE_SecBuf) ps_b; /* buffer list */
53
	STAILQ_ENTRY(_PE_Scn) ps_next;
54
};
55
56
struct _PE {
57
	int		 pe_fd;		/* file descriptor */
58
	PE_Cmd		 pe_cmd;	/* open mode */
59
	PE_Object	 pe_obj;	/* PE32/PE32+/COFF */
60
	size_t		 pe_fsize;	/* file size */
61
	unsigned int	 pe_flags;	/* library flags */
62
	PE_DosHdr	*pe_dh;		/* MS-DOS header */
63
	char		*pe_stub;	/* MS-DOS stub */
64
	size_t		 pe_stub_ex;	/* MS-DOS stub len (exclude hdr) */
65
	char		*pe_stub_app;	/* MS-DOS stub (app supplied) */
66
	size_t		 pe_stub_app_sz; /* MS-DOS stub len (app supplied) */
67
	PE_RichHdr	*pe_rh;		/* rich header */
68
	char		*pe_rh_start;	/* pointer to rich header */
69
	PE_CoffHdr	*pe_ch;		/* COFF header */
70
	PE_OptHdr	*pe_oh;		/* optional header */
71
	PE_DataDir	*pe_dd;		/* data directories */
72
	unsigned int	 pe_nscn;	/* num. of sections */
73
	char		*pe_symtab;	/* COFF symbol table */
74
	size_t		 pe_symbtab_sz; /* size of symbol table */
75
	unsigned int	 pe_nsym;	/* num. of symbols */
76
	unsigned int	 pe_rvamax;	/* maximum RVA */
77
	STAILQ_HEAD(, _PE_Scn) pe_scn;	/* section list */
78
};
79
80
/* Library internal flags  */
81
#define	LIBPE_F_API_MASK		0x000FFFU
82
#define	LIBPE_F_SPECIAL_FILE		0x001000U
83
#define	LIBPE_F_BAD_DOS_HEADER		0x002000U
84
#define	LIBPE_F_BAD_PE_HEADER		0x004000U
85
#define	LIBPE_F_BAD_COFF_HEADER		0x008000U
86
#define	LIBPE_F_BAD_OPT_HEADER		0x010000U
87
#define	LIBPE_F_BAD_SEC_HEADER		0x020000U
88
#define	LIBPE_F_LOAD_DOS_STUB		0x040000U
89
#define	LIBPE_F_FD_DONE			0x080000U
90
#define	LIBPE_F_DIRTY_DOS_HEADER	0x100000U
91
#define	LIBPE_F_DIRTY_COFF_HEADER	0x200000U
92
#define	LIBPE_F_DIRTY_OPT_HEADER	0x400000U
93
#define	LIBPE_F_DIRTY_SEC_HEADER	0x800000U
94
95
/* Internal section flags */
96
#define	LIBPE_F_LOAD_SECTION		0x1000U
97
#define	LIBPE_F_STRIP_SECTION		0x2000U
98
99
/* Internal buffer flags */
100
#define	LIBPE_F_BUFFER_MALLOCED		0x1000U
101
102
/* Library internal defines */
103
#define	PE_DOS_MAGIC		0x5a4dU
104
#define	PE_RICH_TEXT		"Rich"
105
#define	PE_RICH_HIDDEN		0x536e6144U /* DanS */
106
#define	PE_SIGNATURE		0x4550U	   /* PE\0\0 */
107
#define	PE_COFF_OPT_SIZE_32	224
108
#define	PE_COFF_OPT_SIZE_32P	240
109
#define	PE_SYM_ENTRY_SIZE	18
110
111
/* Encode/Decode macros */
112
#if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
113
static  __inline uint16_t
114
le16dec(const void *pp)
115
{
116
	unsigned char const *p = (unsigned char const *)pp;
117
118
	return ((p[1] << 8) | p[0]);
119
}
120
121
static __inline uint32_t
122
le32dec(const void *pp)
123
{
124
	unsigned char const *p = (unsigned char const *)pp;
125
126
	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
127
}
128
129
static __inline uint64_t
130
le64dec(const void *pp)
131
{
132
	unsigned char const *p = (unsigned char const *)pp;
133
134
	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
135
}
136
137
static __inline void
138
le16enc(void *pp, uint16_t u)
139
{
140
	unsigned char *p = (unsigned char *)pp;
141
142
	p[0] = u & 0xff;
143
	p[1] = (u >> 8) & 0xff;
144
}
145
146
static __inline void
147
le32enc(void *pp, uint32_t u)
148
{
149
	unsigned char *p = (unsigned char *)pp;
150
151
	p[0] = u & 0xff;
152
	p[1] = (u >> 8) & 0xff;
153
	p[2] = (u >> 16) & 0xff;
154
	p[3] = (u >> 24) & 0xff;
155
}
156
157
static __inline void
158
le64enc(void *pp, uint64_t u)
159
{
160
	unsigned char *p = (unsigned char *)pp;
161
162
	le32enc(p, (uint32_t)(u & 0xffffffffU));
163
	le32enc(p + 4, (uint32_t)(u >> 32));
164
}
165
#endif	/* ELFTC_NEED_BYTEORDER_EXTENSIONS */
166
167
#define	PE_READ16(p,v)	do {			\
168
	(v) = le16dec((p));			\
169
	(p) += 2;				\
170
} while(0)
171
172
#define	PE_READ32(p,v)	do {			\
173
	(v) = le32dec((p));			\
174
	(p) += 4;				\
175
} while(0)
176
177
#define	PE_WRITE16(p,v) do {			\
178
	le16enc((p), (v));			\
179
	(p) += 2;				\
180
} while(0)
181
182
#define	PE_WRITE32(p,v) do {			\
183
	le32enc((p), (v));			\
184
	(p) += 4;				\
185
} while(0)
186
187
188
/* Internal function declarations */
189
off_t		libpe_align(PE *, off_t, size_t);
190
PE_SecBuf	*libpe_alloc_buffer(PE_Scn *, size_t);
191
PE_Scn		*libpe_alloc_scn(PE *);
192
int		libpe_load_all_sections(PE *);
193
int		libpe_load_section(PE *, PE_Scn *);
194
int		libpe_open_object(PE *);
195
int		libpe_pad(PE *, size_t);
196
int		libpe_parse_msdos_header(PE *, char *);
197
int		libpe_parse_coff_header(PE *, char *);
198
int		libpe_parse_rich_header(PE *);
199
int		libpe_parse_section_headers(PE *);
200
int		libpe_read_msdos_stub(PE *);
201
void		libpe_release_buffer(PE_SecBuf *);
202
void		libpe_release_object(PE *);
203
void		libpe_release_scn(PE_Scn *);
204
size_t		libpe_resync_buffers(PE_Scn *);
205
int		libpe_resync_sections(PE *, off_t);
206
int		libpe_write_buffers(PE_Scn *);
207
off_t		libpe_write_coff_header(PE *, off_t);
208
off_t		libpe_write_msdos_stub(PE *, off_t);
209
off_t		libpe_write_pe_header(PE *, off_t);
210
off_t		libpe_write_sections(PE *, off_t);
211
off_t		libpe_write_section_headers(PE *, off_t);
212
213
#endif	/* !__LIBPE_H_ */
(-)b/contrib/elftoolchain/libpe/libpe.h (+121 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 *
26
 * $Id: libpe.h 3312 2016-01-10 09:23:51Z kaiwang27 $
27
 */
28
29
#ifndef	_LIBPE_H_
30
#define	_LIBPE_H_
31
32
#include <sys/types.h>
33
34
#include "pe.h"
35
36
/* Library private data structures */
37
typedef struct _PE PE;
38
typedef struct _PE_Scn PE_Scn;
39
40
/* Section buffers */
41
typedef struct PE_Buffer {
42
        unsigned int	 pb_align;
43
	off_t		 pb_off;
44
	size_t		 pb_size;
45
	void		*pb_buf;
46
} PE_Buffer;
47
48
/* Object types */
49
typedef enum {
50
	PE_O_UNKNOWN = 0,
51
	PE_O_PE32,
52
	PE_O_PE32P,
53
	PE_O_COFF,
54
} PE_Object;
55
56
/* Commands */
57
typedef enum {
58
	PE_C_NULL = 0,
59
	PE_C_CLR,
60
	PE_C_FDDONE,
61
	PE_C_FDREAD,
62
	PE_C_RDWR,
63
	PE_C_READ,
64
	PE_C_SET,
65
	PE_C_WRITE,
66
	PE_C_NUM
67
} PE_Cmd;
68
69
/* Flags defined by the API. */
70
#define	PE_F_DIRTY		0x001U
71
#define	PE_F_STRIP_DOS_STUB	0x002U
72
#define	PE_F_STRIP_RICH_HEADER	0x004U
73
#define	PE_F_STRIP_SYMTAB	0x008U
74
#define	PE_F_STRIP_DEBUG	0x010U
75
#define	PE_F_STRIP_SECTION	0x020U
76
77
#ifdef __cplusplus
78
extern "C" {
79
#endif
80
81
PE_CoffHdr	*pe_coff_header(PE *);
82
int		pe_cntl(PE *, PE_Cmd);
83
PE_DataDir	*pe_data_dir(PE *);
84
void		pe_finish(PE *);
85
int		pe_flag(PE *, PE_Cmd, unsigned int);
86
int		pe_flag_buffer(PE_Buffer *, PE_Cmd, unsigned int);
87
int		pe_flag_coff_header(PE *, PE_Cmd, unsigned int);
88
int		pe_flag_data_dir(PE *, PE_Cmd, unsigned int);
89
int		pe_flag_dos_header(PE *, PE_Cmd, unsigned int);
90
int		pe_flag_opt_header(PE *, PE_Cmd, unsigned int);
91
int		pe_flag_section_header(PE_Scn *, PE_Cmd, unsigned int);
92
int		pe_flag_scn(PE_Scn *, PE_Cmd, unsigned int);
93
PE_Buffer	*pe_getbuffer(PE_Scn *, PE_Buffer *);
94
PE_Scn 		*pe_getscn(PE *, size_t);
95
PE		*pe_init(int, PE_Cmd, PE_Object);
96
PE_Scn		*pe_insertscn(PE *, size_t);
97
PE_DosHdr	*pe_msdos_header(PE *);
98
char		*pe_msdos_stub(PE *, size_t *);
99
size_t		pe_ndxscn(PE_Scn *);
100
PE_Buffer	*pe_newbuffer(PE_Scn *);
101
PE_Scn		*pe_newscn(PE *);
102
PE_Scn		*pe_nextscn(PE *, PE_Scn *);
103
PE_Object	pe_object(PE *);
104
PE_OptHdr	*pe_opt_header(PE *);
105
PE_RichHdr	*pe_rich_header(PE *);
106
int		pe_rich_header_validate(PE *);
107
PE_SecHdr	*pe_section_header(PE_Scn *);
108
off_t		pe_update(PE *);
109
int		pe_update_coff_header(PE *, PE_CoffHdr *);
110
int		pe_update_opt_header(PE *, PE_OptHdr *);
111
int		pe_update_data_dir(PE *, PE_DataDir *);
112
int		ps_update_msdos_header(PE *, PE_DosHdr *);
113
int		ps_update_msdos_stub(PE *, char *, size_t);
114
int		pe_update_section_header(PE_Scn *, PE_SecHdr *);
115
int		pe_update_symtab(PE *, char *, size_t, unsigned int);
116
117
#ifdef __cplusplus
118
}
119
#endif
120
121
#endif	/* !_LIBPE_H_ */
(-)b/contrib/elftoolchain/libpe/libpe_buffer.c (+185 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/param.h>
28
#include <assert.h>
29
#include <errno.h>
30
#include <stdlib.h>
31
#include <unistd.h>
32
33
#include "_libpe.h"
34
35
ELFTC_VCSID("$Id: libpe_buffer.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
36
37
PE_SecBuf *
38
libpe_alloc_buffer(PE_Scn *ps, size_t sz)
39
{
40
	PE_SecBuf *sb;
41
42
	if ((sb = malloc(sizeof(PE_SecBuf))) == NULL) {
43
		errno = ENOMEM;
44
		return (NULL);
45
	}
46
47
	sb->sb_ps = ps;
48
	sb->sb_flags = 0;
49
	sb->sb_pb.pb_align = 1;
50
	sb->sb_pb.pb_off = 0;
51
	sb->sb_pb.pb_size = sz;
52
	if (sz > 0) {
53
		if ((sb->sb_pb.pb_buf = malloc(sz)) == NULL) {
54
			free(sb);
55
			errno = ENOMEM;
56
			return (NULL);
57
		}
58
		sb->sb_flags |= LIBPE_F_BUFFER_MALLOCED;
59
	} else
60
		sb->sb_pb.pb_buf = NULL;
61
62
	STAILQ_INSERT_TAIL(&ps->ps_b, sb, sb_next);
63
64
	return (sb);
65
}
66
67
void
68
libpe_release_buffer(PE_SecBuf *sb)
69
{
70
	PE_Scn *ps;
71
72
	assert(sb != NULL);
73
74
	ps = sb->sb_ps;
75
76
	STAILQ_REMOVE(&ps->ps_b, sb, _PE_SecBuf, sb_next);
77
78
	if (sb->sb_flags & LIBPE_F_BUFFER_MALLOCED)
79
		free(sb->sb_pb.pb_buf);
80
81
	free(sb);
82
}
83
84
static int
85
cmp_sb(PE_SecBuf *a, PE_SecBuf *b)
86
{
87
88
	if (a->sb_pb.pb_off < b->sb_pb.pb_off)
89
		return (-1);
90
	else if (a->sb_pb.pb_off == b->sb_pb.pb_off)
91
		return (0);
92
	else
93
		return (1);
94
}
95
96
static void
97
sort_buffers(PE_Scn *ps)
98
{
99
100
	if (STAILQ_EMPTY(&ps->ps_b))
101
		return;
102
103
	STAILQ_SORT(&ps->ps_b, _PE_SecBuf, sb_next, cmp_sb);
104
}
105
106
size_t
107
libpe_resync_buffers(PE_Scn *ps)
108
{
109
	PE_SecBuf *sb;
110
	PE_Buffer *pb;
111
	size_t sz;
112
113
	assert(ps->ps_flags & LIBPE_F_LOAD_SECTION);
114
115
	sort_buffers(ps);
116
117
	sz = 0;
118
	STAILQ_FOREACH(sb, &ps->ps_b, sb_next) {
119
		if (ps->ps_flags & PE_F_DIRTY)
120
			sb->sb_flags |= PE_F_DIRTY;
121
122
		pb = (PE_Buffer *) sb;
123
		if (pb->pb_align > ps->ps_falign)
124
			pb->pb_align = ps->ps_falign;
125
		if (pb->pb_buf == NULL || pb->pb_size == 0)
126
			continue;
127
128
		sz = roundup(sz, pb->pb_align);
129
130
		if (pb->pb_off != (off_t) sz) {
131
			pb->pb_off = sz;
132
			sb->sb_flags |= PE_F_DIRTY;
133
		}
134
		sz += pb->pb_size;
135
	}
136
137
	return (sz);
138
}
139
140
int
141
libpe_write_buffers(PE_Scn *ps)
142
{
143
	PE *pe;
144
	PE_SecBuf *sb;
145
	PE_Buffer *pb;
146
	off_t off;
147
148
	assert(ps->ps_flags & LIBPE_F_LOAD_SECTION);
149
150
	pe = ps->ps_pe;
151
152
	off = 0;
153
	STAILQ_FOREACH(sb, &ps->ps_b, sb_next) {
154
		pb = &sb->sb_pb;
155
		if (pb->pb_buf == NULL || pb->pb_size == 0)
156
			continue;
157
158
		if ((sb->sb_flags & PE_F_DIRTY) == 0) {
159
			assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
160
			if (lseek(pe->pe_fd, (off_t) pb->pb_size, SEEK_CUR) <
161
			    0) {
162
				errno = EIO;
163
				return (-1);
164
			}
165
			goto next_buf;
166
		}
167
168
		if (pb->pb_off > off) {
169
			if (libpe_pad(pe, pb->pb_off - off) < 0)
170
				return (-1);
171
			off = pb->pb_off;
172
		}
173
174
		if (write(pe->pe_fd, pb->pb_buf, pb->pb_size) !=
175
		    (ssize_t) pb->pb_size) {
176
			errno = EIO;
177
			return (-1);
178
		}
179
180
	next_buf:
181
		off += pb->pb_size;
182
	}
183
184
	return (0);
185
}
(-)b/contrib/elftoolchain/libpe/libpe_coff.c (+535 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/param.h>
28
#include <assert.h>
29
#include <errno.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <time.h>
33
#include <unistd.h>
34
35
#include "_libpe.h"
36
37
ELFTC_VCSID("$Id: libpe_coff.c 3326 2016-01-16 17:46:17Z kaiwang27 $");
38
39
int
40
libpe_parse_coff_header(PE *pe, char *hdr)
41
{
42
	char tmp[128];
43
	PE_CoffHdr *ch;
44
	PE_OptHdr *oh;
45
	PE_DataDir *dd;
46
	unsigned p, r, s;
47
	int i;
48
49
	if ((ch = malloc(sizeof(PE_CoffHdr))) == NULL) {
50
		errno = ENOMEM;
51
		return (-1);
52
	}
53
54
	PE_READ16(hdr, ch->ch_machine);
55
	PE_READ16(hdr, ch->ch_nsec);
56
	PE_READ32(hdr, ch->ch_timestamp);
57
	PE_READ32(hdr, ch->ch_symptr);
58
	PE_READ32(hdr, ch->ch_nsym);
59
	PE_READ16(hdr, ch->ch_optsize);
60
	PE_READ16(hdr, ch->ch_char);
61
62
	pe->pe_ch = ch;
63
64
	/*
65
	 * The Optional header is omitted for object files.
66
	 */
67
	if (ch->ch_optsize == 0)
68
		return (libpe_parse_section_headers(pe));
69
70
	if ((oh = calloc(1, sizeof(PE_OptHdr))) == NULL) {
71
		errno = ENOMEM;
72
		return (-1);
73
	}
74
	pe->pe_oh = oh;
75
76
#define READ_OPT(n)							\
77
	do {								\
78
		/*							\
79
		 * Since the Optional Header size is variable, we must	\
80
		 * check if the requested read size will overrun the	\
81
		 * remaining header bytes.				\
82
		 */							\
83
		if (p + (n) > ch->ch_optsize) {				\
84
			/* Consume the "extra" bytes */			\
85
			r = ch->ch_optsize - p;				\
86
			if (read(pe->pe_fd, tmp, r) != (ssize_t) r) {	\
87
				pe->pe_flags |= LIBPE_F_BAD_SEC_HEADER;\
88
				return (0);				\
89
			}						\
90
			return (libpe_parse_section_headers(pe));	\
91
		}							\
92
		if (read(pe->pe_fd, tmp, (n)) != (ssize_t) (n)) {	\
93
			pe->pe_flags |= LIBPE_F_BAD_OPT_HEADER;	\
94
			return (0);					\
95
		}							\
96
		p += (n);						\
97
	} while (0)
98
#define	READ_OPT8(v) do { READ_OPT(1); (v) = *tmp; } while(0)
99
#define	READ_OPT16(v) do { READ_OPT(2); (v) = le16dec(tmp); } while(0)
100
#define	READ_OPT32(v) do { READ_OPT(4); (v) = le32dec(tmp); } while(0)
101
#define	READ_OPT64(v) do { READ_OPT(8); (v) = le64dec(tmp); } while(0)
102
103
	/*
104
	 * Read in the Optional header. Size of some fields are depending
105
	 * on the PE format specified by the oh_magic field. (PE32 or PE32+)
106
	 */
107
108
	p = 0;
109
	READ_OPT16(oh->oh_magic);
110
	if (oh->oh_magic == PE_FORMAT_32P)
111
		pe->pe_obj = PE_O_PE32P;
112
	READ_OPT8(oh->oh_ldvermajor);
113
	READ_OPT8(oh->oh_ldverminor);
114
	READ_OPT32(oh->oh_textsize);
115
	READ_OPT32(oh->oh_datasize);
116
	READ_OPT32(oh->oh_bsssize);
117
	READ_OPT32(oh->oh_entry);
118
	READ_OPT32(oh->oh_textbase);
119
	if (oh->oh_magic != PE_FORMAT_32P) {
120
		READ_OPT32(oh->oh_database);
121
		READ_OPT32(oh->oh_imgbase);
122
	} else
123
		READ_OPT64(oh->oh_imgbase);
124
	READ_OPT32(oh->oh_secalign);
125
	READ_OPT32(oh->oh_filealign);
126
	READ_OPT16(oh->oh_osvermajor);
127
	READ_OPT16(oh->oh_osverminor);
128
	READ_OPT16(oh->oh_imgvermajor);
129
	READ_OPT16(oh->oh_imgverminor);
130
	READ_OPT16(oh->oh_subvermajor);
131
	READ_OPT16(oh->oh_subverminor);
132
	READ_OPT32(oh->oh_win32ver);
133
	READ_OPT32(oh->oh_imgsize);
134
	READ_OPT32(oh->oh_hdrsize);
135
	READ_OPT32(oh->oh_checksum);
136
	READ_OPT16(oh->oh_subsystem);
137
	READ_OPT16(oh->oh_dllchar);
138
	if (oh->oh_magic != PE_FORMAT_32P) {
139
		READ_OPT32(oh->oh_stacksizer);
140
		READ_OPT32(oh->oh_stacksizec);
141
		READ_OPT32(oh->oh_heapsizer);
142
		READ_OPT32(oh->oh_heapsizec);
143
	} else {
144
		READ_OPT64(oh->oh_stacksizer);
145
		READ_OPT64(oh->oh_stacksizec);
146
		READ_OPT64(oh->oh_heapsizer);
147
		READ_OPT64(oh->oh_heapsizec);
148
	}
149
	READ_OPT32(oh->oh_ldrflags);
150
	READ_OPT32(oh->oh_ndatadir);
151
152
	/*
153
	 * Read in the Data Directories.
154
	 */
155
156
	if (oh->oh_ndatadir > 0) {
157
		if ((dd = calloc(1, sizeof(PE_DataDir))) == NULL) {
158
			errno = ENOMEM;
159
			return (-1);
160
		}
161
		pe->pe_dd = dd;
162
163
		dd->dd_total = oh->oh_ndatadir < PE_DD_MAX ? oh->oh_ndatadir :
164
			PE_DD_MAX;
165
166
		for (i = 0; (uint32_t) i < dd->dd_total; i++) {
167
			READ_OPT32(dd->dd_e[i].de_addr);
168
			READ_OPT32(dd->dd_e[i].de_size);
169
		}
170
	}
171
172
	/* Consume the remaining bytes in the Optional header, if any. */
173
	if (ch->ch_optsize > p) {
174
		r = ch->ch_optsize - p;
175
		for (; r > 0; r -= s) {
176
			s = r > sizeof(tmp) ? sizeof(tmp) : r;
177
			if (read(pe->pe_fd, tmp, s) != (ssize_t) s) {
178
				pe->pe_flags |= LIBPE_F_BAD_SEC_HEADER;
179
				return (0);
180
			}
181
		}
182
	}
183
184
	return (libpe_parse_section_headers(pe));
185
}
186
187
off_t
188
libpe_write_pe_header(PE *pe, off_t off)
189
{
190
	char tmp[4];
191
192
	if (pe->pe_cmd == PE_C_RDWR &&
193
	    (pe->pe_flags & LIBPE_F_BAD_PE_HEADER) == 0) {
194
		assert(pe->pe_dh != NULL);
195
		off = lseek(pe->pe_fd, (off_t) pe->pe_dh->dh_lfanew + 4,
196
		    SEEK_SET);
197
		return (off);
198
	}
199
200
	/*
201
	 * PE Header should to be aligned on 8-byte boundary according to
202
	 * the PE/COFF specification.
203
	 */
204
	if ((off = libpe_align(pe, off, 8)) < 0)
205
		return (-1);
206
207
	le32enc(tmp, PE_SIGNATURE);
208
	if (write(pe->pe_fd, tmp, sizeof(tmp)) != (ssize_t) sizeof(tmp)) {
209
		errno = EIO;
210
		return (-1);
211
	}
212
213
	off += 4;
214
215
	pe->pe_flags &= ~LIBPE_F_BAD_PE_HEADER;
216
217
	/* Trigger rewrite for the following headers. */
218
	pe->pe_flags |= LIBPE_F_DIRTY_COFF_HEADER;
219
	pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
220
221
	return (off);
222
}
223
224
off_t
225
libpe_write_coff_header(PE *pe, off_t off)
226
{
227
	char tmp[128], *hdr;
228
	PE_CoffHdr *ch;
229
	PE_DataDir *dd;
230
	PE_OptHdr *oh;
231
	PE_Scn *ps;
232
	PE_SecHdr *sh;
233
	unsigned p;
234
	uint32_t reloc_rva, reloc_sz;
235
	int i, reloc;
236
237
	reloc = 0;
238
	reloc_rva = reloc_sz = 0;
239
240
	if (pe->pe_cmd == PE_C_RDWR) {
241
		assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
242
243
		if ((pe->pe_flags & LIBPE_F_DIRTY_COFF_HEADER) == 0 &&
244
		    (pe->pe_flags & LIBPE_F_BAD_COFF_HEADER) == 0) {
245
			if (lseek(pe->pe_fd, (off_t) sizeof(PE_CoffHdr),
246
			    SEEK_CUR) < 0) {
247
				errno = EIO;
248
				return (-1);
249
			}
250
			off += sizeof(PE_CoffHdr);
251
			assert(pe->pe_ch != NULL);
252
			ch = pe->pe_ch;
253
			goto coff_done;
254
		}
255
256
		/* lseek(2) to the offset of the COFF header. */
257
		if (lseek(pe->pe_fd, off, SEEK_SET) < 0) {
258
			errno = EIO;
259
			return (-1);
260
		}
261
	}
262
263
	if (pe->pe_ch == NULL) {
264
		if ((ch = calloc(1, sizeof(PE_CoffHdr))) == NULL) {
265
			errno = ENOMEM;
266
			return (-1);
267
		}
268
		pe->pe_ch = ch;
269
270
		/*
271
		 * Default value for ch_machine if not provided by the
272
		 * application.
273
		 */
274
		if (pe->pe_obj == PE_O_PE32P)
275
			ch->ch_machine = IMAGE_FILE_MACHINE_AMD64;
276
		else
277
			ch->ch_machine = IMAGE_FILE_MACHINE_I386;
278
279
	} else
280
		ch = pe->pe_ch;
281
282
	if (!ch->ch_timestamp)
283
		ch->ch_timestamp = time(NULL);
284
285
	if (pe->pe_obj == PE_O_PE32) {
286
		if (!ch->ch_optsize)
287
			ch->ch_optsize = PE_COFF_OPT_SIZE_32;
288
		ch->ch_char |= IMAGE_FILE_EXECUTABLE_IMAGE |
289
		    IMAGE_FILE_32BIT_MACHINE;
290
	} else if (pe->pe_obj == PE_O_PE32P) {
291
		if (!ch->ch_optsize)
292
			ch->ch_optsize = PE_COFF_OPT_SIZE_32P;
293
		ch->ch_char |= IMAGE_FILE_EXECUTABLE_IMAGE |
294
		    IMAGE_FILE_LARGE_ADDRESS_AWARE;
295
	} else
296
		ch->ch_optsize = 0;
297
298
	/*
299
	 * COFF line number is deprecated by the PE/COFF
300
	 * specification. COFF symbol table is deprecated
301
	 * for executables.
302
	 */
303
	ch->ch_char |= IMAGE_FILE_LINE_NUMS_STRIPPED;
304
	if (pe->pe_obj == PE_O_PE32 || pe->pe_obj == PE_O_PE32P)
305
		ch->ch_char |= IMAGE_FILE_LOCAL_SYMS_STRIPPED;
306
307
	ch->ch_nsec = pe->pe_nscn;
308
309
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
310
		sh = &ps->ps_sh;
311
312
		if (ps->ps_ndx == 0xFFFFFFFFU) {
313
			ch->ch_symptr = sh->sh_rawptr;
314
			ch->ch_nsym = pe->pe_nsym;
315
		}
316
317
		if (pe->pe_obj == PE_O_PE32 || pe->pe_obj == PE_O_PE32P) {
318
			if (ps->ps_ndx == (0xFFFF0000 | PE_DD_BASERELOC) ||
319
			    strncmp(sh->sh_name, ".reloc", strlen(".reloc")) ==
320
			    0) {
321
				reloc = 1;
322
				reloc_rva = sh->sh_addr;
323
				reloc_sz = sh->sh_virtsize;
324
			}
325
		}
326
	}
327
328
	if (!reloc)
329
		ch->ch_char |= IMAGE_FILE_RELOCS_STRIPPED;
330
331
	if (pe->pe_flags & LIBPE_F_BAD_OPT_HEADER) {
332
		if (pe->pe_obj == PE_O_PE32)
333
			ch->ch_optsize = PE_COFF_OPT_SIZE_32;
334
		else if (pe->pe_obj == PE_O_PE32P)
335
			ch->ch_optsize = PE_COFF_OPT_SIZE_32P;
336
		else
337
			ch->ch_optsize = 0;
338
	}
339
340
	/*
341
	 * Write the COFF header.
342
	 */
343
	hdr = tmp;
344
	PE_WRITE16(hdr, ch->ch_machine);
345
	PE_WRITE16(hdr, ch->ch_nsec);
346
	PE_WRITE32(hdr, ch->ch_timestamp);
347
	PE_WRITE32(hdr, ch->ch_symptr);
348
	PE_WRITE32(hdr, ch->ch_nsym);
349
	PE_WRITE16(hdr, ch->ch_optsize);
350
	PE_WRITE16(hdr, ch->ch_char);
351
	if (write(pe->pe_fd, tmp, sizeof(PE_CoffHdr)) !=
352
	    (ssize_t) sizeof(PE_CoffHdr)) {
353
		errno = EIO;
354
		return (-1);
355
	}
356
357
coff_done:
358
	off += sizeof(PE_CoffHdr);
359
	pe->pe_flags &= ~LIBPE_F_DIRTY_COFF_HEADER;
360
	pe->pe_flags &= ~LIBPE_F_BAD_COFF_HEADER;
361
	pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;
362
363
	if (ch->ch_optsize == 0)
364
		return (off);
365
366
	/*
367
	 * Write the Optional header.
368
	 */
369
370
	if (pe->pe_cmd == PE_C_RDWR) {
371
		if ((pe->pe_flags & LIBPE_F_DIRTY_OPT_HEADER) == 0 &&
372
		    (pe->pe_flags & LIBPE_F_BAD_OPT_HEADER) == 0) {
373
			if (lseek(pe->pe_fd, (off_t) ch->ch_optsize,
374
			    SEEK_CUR) < 0) {
375
				errno = EIO;
376
				return (-1);
377
			}
378
			off += ch->ch_optsize;
379
			return (off);
380
		}
381
382
	}
383
384
	if (pe->pe_oh == NULL) {
385
		if ((oh = calloc(1, sizeof(PE_OptHdr))) == NULL) {
386
			errno = ENOMEM;
387
			return (-1);
388
		}
389
		pe->pe_oh = oh;
390
	} else
391
		oh = pe->pe_oh;
392
393
	if (pe->pe_obj == PE_O_PE32)
394
		oh->oh_magic = PE_FORMAT_32;
395
	else
396
		oh->oh_magic = PE_FORMAT_32P;
397
398
	/*
399
	 * LinkerVersion should not be less than 2.5, which will cause
400
	 * Windows to complain the executable is invalid in some case.
401
	 * By default we set LinkerVersion to 2.22 (binutils 2.22)
402
	 */
403
	if (!oh->oh_ldvermajor && !oh->oh_ldverminor) {
404
		oh->oh_ldvermajor = 2;
405
		oh->oh_ldverminor = 22;
406
	}
407
408
	/*
409
	 * The library always tries to write out all 16 data directories
410
	 * but the actual data dir written will depend on ch_optsize.
411
	 */
412
	oh->oh_ndatadir = PE_DD_MAX;
413
414
	if (!oh->oh_filealign)
415
		oh->oh_filealign = 0x200;
416
	if (!oh->oh_secalign)
417
		oh->oh_secalign = 0x1000;
418
	oh->oh_hdrsize = roundup(off + ch->ch_optsize + pe->pe_nscn *
419
	    sizeof(PE_SecHdr), oh->oh_filealign);
420
	oh->oh_imgsize = roundup(pe->pe_rvamax, oh->oh_secalign);
421
422
#define WRITE_OPT(n)							\
423
	do {								\
424
		/*							\
425
		 * Since the Optional Header size is variable, we must	\
426
		 * check if the requested write size will overrun the	\
427
		 * remaining header bytes.				\
428
		 */							\
429
		if (p + (n) > ch->ch_optsize) {				\
430
			/* Pad the "extra" bytes */			\
431
			if (libpe_pad(pe, ch->ch_optsize - p) < 0) {	\
432
				errno = EIO;				\
433
				return (-1);				\
434
			}						\
435
			goto opt_done;					\
436
		}							\
437
		if (write(pe->pe_fd, tmp, (n)) != (ssize_t) (n)) {	\
438
			errno = EIO;					\
439
			return (-1);					\
440
		}							\
441
		p += (n);						\
442
	} while (0)
443
#define	WRITE_OPT8(v) do { *tmp = (v); WRITE_OPT(1); } while(0)
444
#define	WRITE_OPT16(v) do { le16enc(tmp, (v)); WRITE_OPT(2); } while(0)
445
#define	WRITE_OPT32(v) do { le32enc(tmp, (v)); WRITE_OPT(4); } while(0)
446
#define	WRITE_OPT64(v) do { le64enc(tmp, (v)); WRITE_OPT(8); } while(0)
447
448
	p = 0;
449
	WRITE_OPT16(oh->oh_magic);
450
	if (oh->oh_magic == PE_FORMAT_32P)
451
		pe->pe_obj = PE_O_PE32P;
452
	WRITE_OPT8(oh->oh_ldvermajor);
453
	WRITE_OPT8(oh->oh_ldverminor);
454
	WRITE_OPT32(oh->oh_textsize);
455
	WRITE_OPT32(oh->oh_datasize);
456
	WRITE_OPT32(oh->oh_bsssize);
457
	WRITE_OPT32(oh->oh_entry);
458
	WRITE_OPT32(oh->oh_textbase);
459
	if (oh->oh_magic != PE_FORMAT_32P) {
460
		WRITE_OPT32(oh->oh_database);
461
		WRITE_OPT32(oh->oh_imgbase);
462
	} else
463
		WRITE_OPT64(oh->oh_imgbase);
464
	WRITE_OPT32(oh->oh_secalign);
465
	WRITE_OPT32(oh->oh_filealign);
466
	WRITE_OPT16(oh->oh_osvermajor);
467
	WRITE_OPT16(oh->oh_osverminor);
468
	WRITE_OPT16(oh->oh_imgvermajor);
469
	WRITE_OPT16(oh->oh_imgverminor);
470
	WRITE_OPT16(oh->oh_subvermajor);
471
	WRITE_OPT16(oh->oh_subverminor);
472
	WRITE_OPT32(oh->oh_win32ver);
473
	WRITE_OPT32(oh->oh_imgsize);
474
	WRITE_OPT32(oh->oh_hdrsize);
475
	WRITE_OPT32(oh->oh_checksum);
476
	WRITE_OPT16(oh->oh_subsystem);
477
	WRITE_OPT16(oh->oh_dllchar);
478
	if (oh->oh_magic != PE_FORMAT_32P) {
479
		WRITE_OPT32(oh->oh_stacksizer);
480
		WRITE_OPT32(oh->oh_stacksizec);
481
		WRITE_OPT32(oh->oh_heapsizer);
482
		WRITE_OPT32(oh->oh_heapsizec);
483
	} else {
484
		WRITE_OPT64(oh->oh_stacksizer);
485
		WRITE_OPT64(oh->oh_stacksizec);
486
		WRITE_OPT64(oh->oh_heapsizer);
487
		WRITE_OPT64(oh->oh_heapsizec);
488
	}
489
	WRITE_OPT32(oh->oh_ldrflags);
490
	WRITE_OPT32(oh->oh_ndatadir);
491
492
	/*
493
	 * Write the Data Directories.
494
	 */
495
496
	if (oh->oh_ndatadir > 0) {
497
		if (pe->pe_dd == NULL) {
498
			if ((dd = calloc(1, sizeof(PE_DataDir))) == NULL) {
499
				errno = ENOMEM;
500
				return (-1);
501
			}
502
			pe->pe_dd = dd;
503
			dd->dd_total = PE_DD_MAX;
504
		} else
505
			dd = pe->pe_dd;
506
507
		assert(oh->oh_ndatadir <= PE_DD_MAX);
508
509
		if (reloc) {
510
			dd->dd_e[PE_DD_BASERELOC].de_addr = reloc_rva;
511
			dd->dd_e[PE_DD_BASERELOC].de_size = reloc_sz;
512
		}
513
514
		for (i = 0; (uint32_t) i < dd->dd_total; i++) {
515
			WRITE_OPT32(dd->dd_e[i].de_addr);
516
			WRITE_OPT32(dd->dd_e[i].de_size);
517
		}
518
	}
519
520
	/* Pad the remaining bytes in the Optional header, if any. */
521
	if (ch->ch_optsize > p) {
522
		if (libpe_pad(pe, ch->ch_optsize - p) < 0) {
523
			errno = EIO;
524
			return (-1);
525
		}
526
	}
527
528
opt_done:
529
	off += ch->ch_optsize;
530
	pe->pe_flags &= ~LIBPE_F_DIRTY_OPT_HEADER;
531
	pe->pe_flags &= ~LIBPE_F_BAD_OPT_HEADER;
532
	pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;
533
534
	return (off);
535
}
(-)b/contrib/elftoolchain/libpe/libpe_dos.c (+403 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/param.h>
28
#include <sys/types.h>
29
#include <assert.h>
30
#include <errno.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <unistd.h>
34
35
#include "_libpe.h"
36
37
ELFTC_VCSID("$Id: libpe_dos.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
38
39
int
40
libpe_parse_msdos_header(PE *pe, char *hdr)
41
{
42
	PE_DosHdr *dh;
43
	char coff[sizeof(PE_CoffHdr)];
44
	uint32_t pe_magic;
45
	int i;
46
47
	if ((pe->pe_stub = malloc(sizeof(PE_DosHdr))) == NULL) {
48
		errno = ENOMEM;
49
		return (-1);
50
	}
51
	memcpy(pe->pe_stub, hdr, sizeof(PE_DosHdr));
52
53
	if ((dh = malloc(sizeof(*dh))) == NULL) {
54
		errno = ENOMEM;
55
		return (-1);
56
	}
57
	pe->pe_dh = dh;
58
59
	/* Read the conventional MS-DOS EXE header. */
60
	memcpy(dh->dh_magic, hdr, 2);
61
	hdr += 2;
62
	PE_READ16(hdr, dh->dh_lastsize);
63
	PE_READ16(hdr, dh->dh_nblock);
64
	PE_READ16(hdr, dh->dh_nreloc);
65
	PE_READ16(hdr, dh->dh_hdrsize);
66
	PE_READ16(hdr, dh->dh_minalloc);
67
	PE_READ16(hdr, dh->dh_maxalloc);
68
	PE_READ16(hdr, dh->dh_ss);
69
	PE_READ16(hdr, dh->dh_sp);
70
	PE_READ16(hdr, dh->dh_checksum);
71
	PE_READ16(hdr, dh->dh_ip);
72
	PE_READ16(hdr, dh->dh_cs);
73
	PE_READ16(hdr, dh->dh_relocpos);
74
	PE_READ16(hdr, dh->dh_noverlay);
75
76
	/* Do not continue if the EXE is not a PE/NE/... (new executable) */
77
	if (dh->dh_relocpos != 0x40) {
78
		pe->pe_flags |= LIBPE_F_BAD_DOS_HEADER;
79
		return (0);
80
	}
81
82
	for (i = 0; i < 4; i++)
83
		PE_READ16(hdr, dh->dh_reserved1[i]);
84
	PE_READ16(hdr, dh->dh_oemid);
85
	PE_READ16(hdr, dh->dh_oeminfo);
86
	for (i = 0; i < 10; i++)
87
		PE_READ16(hdr, dh->dh_reserved2[i]);
88
	PE_READ32(hdr, dh->dh_lfanew);
89
90
	/* Check if the e_lfanew pointer is valid. */
91
	if (dh->dh_lfanew > pe->pe_fsize - 4) {
92
		pe->pe_flags |= LIBPE_F_BAD_DOS_HEADER;
93
		return (0);
94
	}
95
96
	if (dh->dh_lfanew < sizeof(PE_DosHdr) &&
97
	    (pe->pe_flags & LIBPE_F_SPECIAL_FILE)) {
98
		pe->pe_flags |= LIBPE_F_BAD_DOS_HEADER;
99
		return (0);
100
	}
101
102
	if (dh->dh_lfanew > sizeof(PE_DosHdr)) {
103
		pe->pe_stub_ex = dh->dh_lfanew - sizeof(PE_DosHdr);
104
		if (pe->pe_flags & LIBPE_F_SPECIAL_FILE) {
105
			/* Read in DOS stub now. */
106
			if (libpe_read_msdos_stub(pe) < 0) {
107
				pe->pe_flags |= LIBPE_F_BAD_DOS_HEADER;
108
				return (0);
109
			}
110
		}
111
	}
112
113
	if ((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0) {
114
		/* Jump to the PE header. */
115
		if (lseek(pe->pe_fd, (off_t) dh->dh_lfanew, SEEK_SET) < 0) {
116
			pe->pe_flags |= LIBPE_F_BAD_PE_HEADER;
117
			return (0);
118
		}
119
	}
120
121
	if (read(pe->pe_fd, &pe_magic, 4) != 4 ||
122
	    htole32(pe_magic) != PE_SIGNATURE) {
123
		pe->pe_flags |= LIBPE_F_BAD_PE_HEADER;
124
		return (0);
125
	}
126
127
	if (read(pe->pe_fd, coff, sizeof(coff)) != (ssize_t) sizeof(coff)) {
128
		pe->pe_flags |= LIBPE_F_BAD_COFF_HEADER;
129
		return (0);
130
	}
131
132
	return (libpe_parse_coff_header(pe, coff));
133
}
134
135
int
136
libpe_read_msdos_stub(PE *pe)
137
{
138
	void *m;
139
140
	assert(pe->pe_stub_ex > 0 &&
141
	    (pe->pe_flags & LIBPE_F_LOAD_DOS_STUB) == 0);
142
143
	if ((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0) {
144
		if (lseek(pe->pe_fd, (off_t) sizeof(PE_DosHdr), SEEK_SET) <
145
		    0) {
146
			errno = EIO;
147
			goto fail;
148
		}
149
	}
150
151
	if ((m = realloc(pe->pe_stub, sizeof(PE_DosHdr) + pe->pe_stub_ex)) ==
152
	    NULL) {
153
		errno = ENOMEM;
154
		goto fail;
155
	}
156
	pe->pe_stub = m;
157
158
	if (read(pe->pe_fd, pe->pe_stub + sizeof(PE_DosHdr), pe->pe_stub_ex) !=
159
	    (ssize_t) pe->pe_stub_ex) {
160
		errno = EIO;
161
		goto fail;
162
	}
163
164
	pe->pe_flags |= LIBPE_F_LOAD_DOS_STUB;
165
166
	/* Search for the Rich header embedded just before the PE header. */
167
	(void) libpe_parse_rich_header(pe);
168
169
	return (0);
170
171
fail:
172
	pe->pe_stub_ex = 0;
173
174
	return (-1);
175
}
176
177
/*
178
 * The "standard" MS-DOS stub displaying "This program cannot be run in
179
 * DOS mode".
180
 */
181
static const char msdos_stub[] = {
182
    '\x0e','\x1f','\xba','\x0e','\x00','\xb4','\x09','\xcd',
183
    '\x21','\xb8','\x01','\x4c','\xcd','\x21','\x54','\x68',
184
    '\x69','\x73','\x20','\x70','\x72','\x6f','\x67','\x72',
185
    '\x61','\x6d','\x20','\x63','\x61','\x6e','\x6e','\x6f',
186
    '\x74','\x20','\x62','\x65','\x20','\x72','\x75','\x6e',
187
    '\x20','\x69','\x6e','\x20','\x44','\x4f','\x53','\x20',
188
    '\x6d','\x6f','\x64','\x65','\x2e','\x0d','\x0d','\x0a',
189
    '\x24','\x00','\x00','\x00','\x00','\x00','\x00','\x00',
190
};
191
192
static void
193
init_dos_header(PE_DosHdr *dh)
194
{
195
196
	dh->dh_magic[0] = 'M';
197
	dh->dh_magic[1] = 'Z';
198
	dh->dh_lastsize = 144;
199
	dh->dh_nblock = 3;
200
	dh->dh_hdrsize = 4;
201
	dh->dh_maxalloc = 65535;
202
	dh->dh_sp = 184;
203
	dh->dh_relocpos = 0x40;
204
	dh->dh_lfanew = 0x80;
205
}
206
207
off_t
208
libpe_write_msdos_stub(PE *pe, off_t off)
209
{
210
	PE_DosHdr *dh;
211
	char tmp[sizeof(PE_DosHdr)], *hdr;
212
	off_t d;
213
	int i, strip_rich;
214
215
	strip_rich = 0;
216
217
	if (pe->pe_cmd == PE_C_RDWR) {
218
		assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
219
220
		if (pe->pe_dh != NULL &&
221
		    (pe->pe_flags & PE_F_STRIP_DOS_STUB)) {
222
			/*
223
			 * If we strip MS-DOS stub, everything after it
224
			 * needs rewritten.
225
			 */
226
			pe->pe_flags |= LIBPE_F_BAD_PE_HEADER;
227
			goto done;
228
		}
229
230
		/*
231
		 * lseek(2) to the PE signature if MS-DOS stub is not
232
		 * modified.
233
		 */
234
		if (pe->pe_dh != NULL &&
235
		    (pe->pe_flags & LIBPE_F_DIRTY_DOS_HEADER) == 0 &&
236
		    (pe->pe_flags & LIBPE_F_BAD_DOS_HEADER) == 0 &&
237
		    (pe->pe_flags & PE_F_STRIP_RICH_HEADER) == 0) {
238
			if (lseek(pe->pe_fd,
239
			    (off_t) (sizeof(PE_DosHdr) + pe->pe_stub_ex),
240
			    SEEK_CUR) < 0) {
241
				errno = EIO;
242
				return (-1);
243
			}
244
			off = sizeof(PE_DosHdr) + pe->pe_stub_ex;
245
			goto done;
246
		}
247
248
		/* Check if we should strip the Rich header. */
249
		if (pe->pe_dh != NULL && pe->pe_stub_app == NULL &&
250
		    (pe->pe_flags & LIBPE_F_BAD_DOS_HEADER) == 0 &&
251
		    (pe->pe_flags & PE_F_STRIP_RICH_HEADER)) {
252
			if ((pe->pe_flags & LIBPE_F_LOAD_DOS_STUB) == 0) {
253
				(void) libpe_read_msdos_stub(pe);
254
				if (lseek(pe->pe_fd, off, SEEK_SET) < 0) {
255
					errno = EIO;
256
					return (-1);
257
				}
258
			}
259
			if (pe->pe_rh != NULL) {
260
				strip_rich = 1;
261
				pe->pe_flags |= LIBPE_F_DIRTY_DOS_HEADER;
262
			}
263
		}
264
265
		/*
266
		 * If length of MS-DOS stub will change, Mark the PE
267
		 * signature is broken so that the PE signature and the
268
		 * headers follow it will be rewritten.
269
		 *
270
		 * The sections should be loaded now since the stub might
271
		 * overwrite the section data.
272
		 */
273
		if ((pe->pe_flags & LIBPE_F_BAD_DOS_HEADER) ||
274
		    (pe->pe_stub_app != NULL && pe->pe_stub_app_sz !=
275
			sizeof(PE_DosHdr) + pe->pe_stub_ex) || strip_rich) {
276
			if (libpe_load_all_sections(pe) < 0)
277
				return (-1);
278
			if (lseek(pe->pe_fd, off, SEEK_SET) < 0) {
279
				errno = EIO;
280
				return (-1);
281
			}
282
			pe->pe_flags |= LIBPE_F_BAD_PE_HEADER;
283
		}
284
	}
285
286
	if (pe->pe_flags & PE_F_STRIP_DOS_STUB)
287
		goto done;
288
289
	/* Always use application supplied MS-DOS stub, if exists. */
290
	if (pe->pe_stub_app != NULL && pe->pe_stub_app_sz > 0) {
291
		if (write(pe->pe_fd, pe->pe_stub_app, pe->pe_stub_app_sz) !=
292
		    (ssize_t) pe->pe_stub_app_sz) {
293
			errno = EIO;
294
			return (-1);
295
		}
296
		off = pe->pe_stub_app_sz;
297
		goto done;
298
	}
299
300
	/*
301
	 * Write MS-DOS header.
302
	 */
303
304
	if (pe->pe_dh == NULL) {
305
		if ((dh = calloc(1, sizeof(PE_DosHdr))) == NULL) {
306
			errno = ENOMEM;
307
			return (-1);
308
		}
309
		pe->pe_dh = dh;
310
311
		init_dos_header(dh);
312
313
		pe->pe_flags |= LIBPE_F_DIRTY_DOS_HEADER;
314
	} else
315
		dh = pe->pe_dh;
316
317
	if (pe->pe_flags & LIBPE_F_BAD_DOS_HEADER)
318
		init_dos_header(dh);
319
320
	if (strip_rich) {
321
		d = pe->pe_rh_start - pe->pe_stub;
322
		dh->dh_lfanew = roundup(d, 8);
323
	}
324
325
	if ((pe->pe_flags & LIBPE_F_DIRTY_DOS_HEADER) ||
326
	    (pe->pe_flags & LIBPE_F_BAD_DOS_HEADER)) {
327
		memcpy(tmp, dh->dh_magic, 2);
328
		hdr = tmp + 2;
329
		PE_WRITE16(hdr, dh->dh_lastsize);
330
		PE_WRITE16(hdr, dh->dh_nblock);
331
		PE_WRITE16(hdr, dh->dh_nreloc);
332
		PE_WRITE16(hdr, dh->dh_hdrsize);
333
		PE_WRITE16(hdr, dh->dh_minalloc);
334
		PE_WRITE16(hdr, dh->dh_maxalloc);
335
		PE_WRITE16(hdr, dh->dh_ss);
336
		PE_WRITE16(hdr, dh->dh_sp);
337
		PE_WRITE16(hdr, dh->dh_checksum);
338
		PE_WRITE16(hdr, dh->dh_ip);
339
		PE_WRITE16(hdr, dh->dh_cs);
340
		PE_WRITE16(hdr, dh->dh_relocpos);
341
		PE_WRITE16(hdr, dh->dh_noverlay);
342
		for (i = 0; i < 4; i++)
343
			PE_WRITE16(hdr, dh->dh_reserved1[i]);
344
		PE_WRITE16(hdr, dh->dh_oemid);
345
		PE_WRITE16(hdr, dh->dh_oeminfo);
346
		for (i = 0; i < 10; i++)
347
			PE_WRITE16(hdr, dh->dh_reserved2[i]);
348
		PE_WRITE32(hdr, dh->dh_lfanew);
349
350
		if (write(pe->pe_fd, tmp, sizeof(tmp)) !=
351
		    (ssize_t) sizeof(tmp)) {
352
			errno = EIO;
353
			return (-1);
354
		}
355
	} else {
356
		assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
357
		if (lseek(pe->pe_fd, (off_t) sizeof(PE_DosHdr), SEEK_CUR) <
358
		    0) {
359
			errno = EIO;
360
			return (-1);
361
		}
362
	}
363
364
	off = sizeof(PE_DosHdr);
365
366
	/*
367
	 * Write the MS-DOS stub.
368
	 */
369
370
	if (strip_rich) {
371
		assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
372
		assert(pe->pe_stub != NULL && pe->pe_rh_start != NULL);
373
		d = pe->pe_rh_start - pe->pe_stub;
374
		if (lseek(pe->pe_fd, d, SEEK_SET) < 0) {
375
			errno = EIO;
376
			return (-1);
377
		}
378
		off = d;
379
		goto done;
380
	}
381
382
	if (pe->pe_cmd == PE_C_RDWR) {
383
		if (lseek(pe->pe_fd, (off_t) pe->pe_stub_ex, SEEK_CUR) < 0) {
384
			errno = EIO;
385
			return (-1);
386
		}
387
		off += pe->pe_stub_ex;
388
		goto done;
389
	}
390
391
	if (write(pe->pe_fd, msdos_stub, sizeof(msdos_stub)) !=
392
	    (ssize_t) sizeof(msdos_stub)) {
393
		errno = EIO;
394
		return (-1);
395
	}
396
	off += sizeof(msdos_stub);
397
398
done:
399
	pe->pe_flags &= ~LIBPE_F_DIRTY_DOS_HEADER;
400
	pe->pe_flags &= ~LIBPE_F_BAD_DOS_HEADER;
401
402
	return (off);
403
}
(-)b/contrib/elftoolchain/libpe/libpe_init.c (+145 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/stat.h>
28
#include <assert.h>
29
#include <errno.h>
30
#include <stdlib.h>
31
#include <unistd.h>
32
33
#include "_libpe.h"
34
35
ELFTC_VCSID("$Id: libpe_init.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
36
37
int
38
libpe_open_object(PE *pe)
39
{
40
	struct stat sb;
41
	mode_t mode;
42
	char magic[sizeof(PE_DosHdr)];
43
44
	if (fstat(pe->pe_fd, &sb) < 0)
45
		return (-1);
46
47
	mode = sb.st_mode;
48
	pe->pe_fsize = (size_t) sb.st_size;
49
50
	/* Reject unsupported file types. */
51
	if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
52
	    !S_ISSOCK(mode)) {
53
		errno = EINVAL;
54
		return (-1);
55
	}
56
57
	/* Read/Write mode is not supported for non-regular file. */
58
	if (pe->pe_cmd == PE_C_RDWR && !S_ISREG(mode)) {
59
		errno = EINVAL;
60
		return (-1);
61
	}
62
63
	/* The minimal file should at least contain a COFF header. */
64
	if (S_ISREG(mode) && pe->pe_fsize < sizeof(PE_CoffHdr)) {
65
		errno = ENOENT;
66
		return (-1);
67
	}
68
69
	/*
70
	 * Search for MS-DOS header or COFF header.
71
	 */
72
73
	if (read(pe->pe_fd, magic, 2) != 2) {
74
		errno = EIO;
75
		return (-1);
76
	}
77
78
	if (magic[0] == 'M' && magic[1] == 'Z') {
79
		pe->pe_obj = PE_O_PE32;
80
		if (read(pe->pe_fd, &magic[2], sizeof(PE_DosHdr) - 2) !=
81
		    (ssize_t) sizeof(PE_DosHdr) - 2) {
82
			errno = EIO;
83
			return (-1);
84
		}
85
		return (libpe_parse_msdos_header(pe, magic));
86
87
	} else if (magic[0] == 'P' && magic[1] == 'E') {
88
		if (read(pe->pe_fd, magic, 2) != 2) {
89
			errno = EIO;
90
			return (-1);
91
		}
92
		if (magic[0] == '\0' && magic[1] == '\0') {
93
			pe->pe_obj = PE_O_PE32;
94
			if (read(pe->pe_fd, magic, sizeof(PE_CoffHdr)) !=
95
			    (ssize_t) sizeof(PE_CoffHdr)) {
96
				errno = EIO;
97
				return (-1);
98
			}
99
			return (libpe_parse_coff_header(pe, magic));
100
		}
101
		errno = ENOENT;
102
		return (-1);
103
104
	} else {
105
		pe->pe_obj = PE_O_COFF;
106
		if (read(pe->pe_fd, &magic[2], sizeof(PE_CoffHdr) - 2) !=
107
		    (ssize_t) sizeof(PE_CoffHdr) - 2) {
108
			errno = EIO;
109
			return (-1);
110
		}
111
		return (libpe_parse_coff_header(pe, magic));
112
	}
113
}
114
115
void
116
libpe_release_object(PE *pe)
117
{
118
	PE_Scn *ps, *_ps;
119
120
	if (pe->pe_dh)
121
		free(pe->pe_dh);
122
123
	if (pe->pe_rh) {
124
		free(pe->pe_rh->rh_compid);
125
		free(pe->pe_rh->rh_cnt);
126
		free(pe->pe_rh);
127
	}
128
129
	if (pe->pe_ch)
130
		free(pe->pe_ch);
131
132
	if (pe->pe_oh)
133
		free(pe->pe_oh);
134
135
	if (pe->pe_dd)
136
		free(pe->pe_dd);
137
138
	if (pe->pe_stub)
139
		free(pe->pe_stub);
140
141
	STAILQ_FOREACH_SAFE(ps, &pe->pe_scn, ps_next, _ps)
142
		libpe_release_scn(ps);
143
144
	free(pe);
145
}
(-)b/contrib/elftoolchain/libpe/libpe_rich.c (+128 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <assert.h>
28
#include <errno.h>
29
#include <stdlib.h>
30
#include <string.h>
31
32
#include "_libpe.h"
33
34
ELFTC_VCSID("$Id: libpe_rich.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
35
36
static char *
37
memfind(char *s, const char *find, size_t slen, size_t flen)
38
{
39
	int i;
40
41
	if (slen == 0 || flen == 0 || flen > slen)
42
		return (NULL);
43
44
	for (i = 0; (size_t) i <= slen - flen; i++) {
45
		if (s[i] != find[0])
46
			continue;
47
		if (flen == 1)
48
			return (&s[i]);
49
		if (memcmp(&s[i + 1], &find[1], flen - 1) == 0)
50
			return (&s[i]);
51
	}
52
53
	return (NULL);
54
}
55
56
int
57
libpe_parse_rich_header(PE *pe)
58
{
59
	PE_RichHdr *rh;
60
	char *p, *r, *s;
61
	uint32_t x;
62
	int found, i;
63
64
	assert(pe->pe_stub != NULL && pe->pe_stub_ex > 0);
65
66
	/* Search for the "Rich" keyword to locate the Rich header. */
67
	s = pe->pe_stub + sizeof(PE_DosHdr);
68
	r = memfind(s, PE_RICH_TEXT, pe->pe_stub_ex, 4);
69
	if (r == NULL || r + 8 > s + pe->pe_stub_ex) {
70
		errno = ENOENT;
71
		return (-1);
72
	}
73
74
	if ((rh = calloc(1, sizeof(*rh))) == NULL) {
75
		errno = ENOMEM;
76
		return (-1);
77
	}
78
79
	rh->rh_xor = le32dec(r + 4); /* Retrieve the "XOR mask" */
80
81
	/*
82
	 * Search for the hidden keyword "DanS" by XOR the dwords before
83
	 * the "Rich" keyword with the XOR mask.
84
	 */
85
	found = 0;
86
	for (p = r - 4; p >= s; p -= 4) {
87
		x = le32dec(p) ^ rh->rh_xor;
88
		if (x == PE_RICH_HIDDEN) {
89
			found = 1;
90
			break;
91
		}
92
	}
93
	if (!found) {
94
		free(rh);
95
		errno = ENOENT;
96
		return (-1);
97
	}
98
99
	/*
100
	 * Found the "DanS" keyword, which is the start of the Rich header.
101
	 * The next step is to skip the first 16 bytes (DanS, XOR mask,
102
	 * XOR mask, XOR mask) and read the (compid,cnt) tuples.
103
	 */
104
	pe->pe_rh_start = p;
105
	p += 16;
106
	rh->rh_total = (r - p) / 8;
107
	if ((rh->rh_compid = malloc(rh->rh_total * sizeof(*rh->rh_compid))) ==
108
	    NULL) {
109
		free(rh);
110
		errno = ENOMEM;
111
		return (-1);
112
	}
113
	if ((rh->rh_cnt = malloc(rh->rh_total * sizeof(*rh->rh_cnt))) ==
114
	    NULL) {
115
		free(rh->rh_compid);
116
		free(rh);
117
		errno = ENOMEM;
118
		return (-1);
119
	}
120
	for (i = 0; (uint32_t) i < rh->rh_total; i++, p += 8) {
121
		rh->rh_compid[i] = le32dec(p) ^ rh->rh_xor;
122
		rh->rh_cnt[i] = le32dec(p + 4) ^ rh->rh_xor;
123
	}
124
125
	pe->pe_rh = rh;
126
127
	return (0);
128
}
(-)b/contrib/elftoolchain/libpe/libpe_section.c (+518 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/param.h>
28
#include <assert.h>
29
#include <errno.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <unistd.h>
33
34
#include "_libpe.h"
35
36
ELFTC_VCSID("$Id: libpe_section.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
37
38
PE_Scn *
39
libpe_alloc_scn(PE *pe)
40
{
41
	PE_Scn *ps;
42
43
	if ((ps = calloc(1, sizeof(PE_Scn))) == NULL) {
44
		errno = ENOMEM;
45
		return (NULL);
46
	}
47
	STAILQ_INIT(&ps->ps_b);
48
	ps->ps_pe = pe;
49
50
	return (ps);
51
}
52
53
void
54
libpe_release_scn(PE_Scn *ps)
55
{
56
	PE *pe;
57
	PE_SecBuf *sb, *_sb;
58
59
	assert(ps != NULL);
60
61
	pe = ps->ps_pe;
62
63
	STAILQ_REMOVE(&pe->pe_scn, ps, _PE_Scn, ps_next);
64
65
	STAILQ_FOREACH_SAFE(sb, &ps->ps_b, sb_next, _sb)
66
		libpe_release_buffer(sb);
67
68
	free(ps);
69
}
70
71
static int
72
cmp_scn(PE_Scn *a, PE_Scn *b)
73
{
74
75
	if (a->ps_sh.sh_addr < b->ps_sh.sh_addr)
76
		return (-1);
77
	else if (a->ps_sh.sh_addr == b->ps_sh.sh_addr)
78
		return (0);
79
	else
80
		return (1);
81
}
82
83
static void
84
sort_sections(PE *pe)
85
{
86
87
	if (STAILQ_EMPTY(&pe->pe_scn))
88
		return;
89
90
	/* Sort the list of Scn by RVA in ascending order. */
91
	STAILQ_SORT(&pe->pe_scn, _PE_Scn, ps_next, cmp_scn);
92
}
93
94
int
95
libpe_parse_section_headers(PE *pe)
96
{
97
	char tmp[sizeof(PE_SecHdr)], *hdr;
98
	PE_Scn *ps;
99
	PE_SecHdr *sh;
100
	PE_CoffHdr *ch;
101
	PE_DataDir *dd;
102
	int found, i;
103
104
	assert(pe->pe_ch != NULL);
105
106
	for (i = 0; (uint16_t) i < pe->pe_ch->ch_nsec; i++) {
107
		if (read(pe->pe_fd, tmp, sizeof(PE_SecHdr)) !=
108
		    (ssize_t) sizeof(PE_SecHdr)) {
109
			pe->pe_flags |= LIBPE_F_BAD_SEC_HEADER;
110
			return (0);
111
		}
112
113
		if ((ps = libpe_alloc_scn(pe)) == NULL)
114
			return (-1);
115
		STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);
116
		ps->ps_ndx = ++pe->pe_nscn;	/* Setion index is 1-based */
117
		sh = &ps->ps_sh;
118
119
		/*
120
		 * Note that the section name won't be NUL-terminated if
121
		 * its length happens to be 8.
122
		 */
123
		memcpy(sh->sh_name, tmp, sizeof(sh->sh_name));
124
		hdr = tmp + 8;
125
		PE_READ32(hdr, sh->sh_virtsize);
126
		PE_READ32(hdr, sh->sh_addr);
127
		PE_READ32(hdr, sh->sh_rawsize);
128
		PE_READ32(hdr, sh->sh_rawptr);
129
		PE_READ32(hdr, sh->sh_relocptr);
130
		PE_READ32(hdr, sh->sh_lineptr);
131
		PE_READ16(hdr, sh->sh_nreloc);
132
		PE_READ16(hdr, sh->sh_nline);
133
		PE_READ32(hdr, sh->sh_char);
134
	}
135
136
	/*
137
	 * For all the data directories that don't belong to any section,
138
	 * we create pseudo sections for them to make layout easier.
139
	 */
140
	dd = pe->pe_dd;
141
	if (dd != NULL && dd->dd_total > 0) {
142
		for (i = 0; (uint32_t) i < pe->pe_dd->dd_total; i++) {
143
			if (dd->dd_e[i].de_size == 0)
144
				continue;
145
			found = 0;
146
			STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
147
				sh = &ps->ps_sh;
148
				if (dd->dd_e[i].de_addr >= sh->sh_addr &&
149
				    dd->dd_e[i].de_addr + dd->dd_e[i].de_size <=
150
				    sh->sh_addr + sh->sh_virtsize) {
151
					found = 1;
152
					break;
153
				}
154
			}
155
			if (found)
156
				continue;
157
158
			if ((ps = libpe_alloc_scn(pe)) == NULL)
159
				return (-1);
160
			STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);
161
			ps->ps_ndx = 0xFFFF0000U | i;
162
			sh = &ps->ps_sh;
163
			sh->sh_rawptr = dd->dd_e[i].de_addr; /* FIXME */
164
			sh->sh_rawsize = dd->dd_e[i].de_size;
165
		}
166
	}
167
168
	/*
169
	 * Also consider the COFF symbol table as a pseudo section.
170
	 */
171
	ch = pe->pe_ch;
172
	if (ch->ch_nsym > 0) {
173
		if ((ps = libpe_alloc_scn(pe)) == NULL)
174
			return (-1);
175
		STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);
176
		ps->ps_ndx = 0xFFFFFFFFU;
177
		sh = &ps->ps_sh;
178
		sh->sh_rawptr = ch->ch_symptr;
179
		sh->sh_rawsize = ch->ch_nsym * PE_SYM_ENTRY_SIZE;
180
		pe->pe_nsym = ch->ch_nsym;
181
	}
182
183
	/* PE file headers initialization is complete if we reach here. */
184
	return (0);
185
}
186
187
int
188
libpe_load_section(PE *pe, PE_Scn *ps)
189
{
190
	PE_SecHdr *sh;
191
	PE_SecBuf *sb;
192
	size_t sz;
193
	char tmp[4];
194
195
	assert(pe != NULL && ps != NULL);
196
	assert((ps->ps_flags & LIBPE_F_LOAD_SECTION) == 0);
197
198
	sh = &ps->ps_sh;
199
200
	/* Allocate a PE_SecBuf struct without buffer for empty sections. */
201
	if (sh->sh_rawsize == 0) {
202
		(void) libpe_alloc_buffer(ps, 0);
203
		ps->ps_flags |= LIBPE_F_LOAD_SECTION;
204
		return (0);
205
	}
206
207
	if ((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0) {
208
		if (lseek(pe->pe_fd, (off_t) sh->sh_rawptr, SEEK_SET) < 0) {
209
			errno = EIO;
210
			return (-1);
211
		}
212
	}
213
214
	if ((sb = libpe_alloc_buffer(ps, sh->sh_rawsize)) == NULL)
215
		return (-1);
216
217
	if (read(pe->pe_fd, sb->sb_pb.pb_buf, sh->sh_rawsize) !=
218
	    (ssize_t) sh->sh_rawsize) {
219
		errno = EIO;
220
		return (-1);
221
	}
222
223
	if (ps->ps_ndx == 0xFFFFFFFFU) {
224
		/*
225
		 * Index 0xFFFFFFFF indicates this section is a pseudo
226
		 * section that contains the COFF symbol table. We should
227
		 * read in the string table right after it.
228
		 */
229
		if (read(pe->pe_fd, tmp, sizeof(tmp)) !=
230
		    (ssize_t) sizeof(tmp)) {
231
			errno = EIO;
232
			return (-1);
233
		}
234
		sz = le32dec(tmp);
235
236
		/*
237
		 * The minimum value for the size field is 4, which indicates
238
		 * there is no string table.
239
		 */
240
		if (sz > 4) {
241
			sz -= 4;
242
			if ((sb = libpe_alloc_buffer(ps, sz)) == NULL)
243
				return (-1);
244
			if (read(pe->pe_fd, sb->sb_pb.pb_buf, sz) !=
245
			    (ssize_t) sz) {
246
				errno = EIO;
247
				return (-1);
248
			}
249
		}
250
	}
251
252
	ps->ps_flags |= LIBPE_F_LOAD_SECTION;
253
254
	return (0);
255
}
256
257
int
258
libpe_load_all_sections(PE *pe)
259
{
260
	PE_Scn *ps;
261
	PE_SecHdr *sh;
262
	unsigned r, s;
263
	off_t off;
264
	char tmp[256];
265
266
	/* Calculate the current offset into the file. */
267
	off = 0;
268
	if (pe->pe_dh != NULL)
269
		off += pe->pe_dh->dh_lfanew + 4;
270
	if (pe->pe_ch != NULL)
271
		off += sizeof(PE_CoffHdr) + pe->pe_ch->ch_optsize;
272
273
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
274
		if (ps->ps_flags & LIBPE_F_LOAD_SECTION)
275
			continue;
276
		sh = &ps->ps_sh;
277
278
		/*
279
		 * For special files, we consume the padding in between
280
		 * and advance to the section offset.
281
		 */
282
		if (pe->pe_flags & LIBPE_F_SPECIAL_FILE) {
283
			/* Can't go backwards. */
284
			if (off > sh->sh_rawptr) {
285
				errno = EIO;
286
				return (-1);
287
			}
288
			if (off < sh->sh_rawptr) {
289
				r = sh->sh_rawptr - off;
290
				for (; r > 0; r -= s) {
291
					s = r > sizeof(tmp) ? sizeof(tmp) : r;
292
					if (read(pe->pe_fd, tmp, s) !=
293
					    (ssize_t) s) {
294
						errno = EIO;
295
						return (-1);
296
					}
297
				}
298
			}
299
		}
300
301
		/* Load the section content. */
302
		if (libpe_load_section(pe, ps) < 0)
303
			return (-1);
304
	}
305
306
	return (0);
307
}
308
309
int
310
libpe_resync_sections(PE *pe, off_t off)
311
{
312
	PE_Scn *ps;
313
	PE_SecHdr *sh;
314
	size_t falign, nsec;
315
316
	/* Firstly, sort all sections by their file offsets. */
317
	sort_sections(pe);
318
319
	/* Count the number of sections. */
320
	nsec = 0;
321
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
322
		if (ps->ps_flags & LIBPE_F_STRIP_SECTION)
323
			continue;
324
		if (ps->ps_ndx & 0xFFFF0000U)
325
			continue;
326
		nsec++;
327
	}
328
	pe->pe_nscn = nsec;
329
330
	/*
331
	 * Calculate the file offset for the first section. (`off' is
332
	 * currently pointing to the COFF header.)
333
	 */
334
	off += sizeof(PE_CoffHdr);
335
	if (pe->pe_ch != NULL && pe->pe_ch->ch_optsize > 0)
336
		off += pe->pe_ch->ch_optsize;
337
	else {
338
		switch (pe->pe_obj) {
339
		case PE_O_PE32:
340
			off += PE_COFF_OPT_SIZE_32;
341
			break;
342
		case PE_O_PE32P:
343
			off += PE_COFF_OPT_SIZE_32P;
344
			break;
345
		case PE_O_COFF:
346
		default:
347
			break;
348
		}
349
	}
350
	off += nsec * sizeof(PE_SecHdr);
351
352
	/*
353
	 * Determine the file alignment for sections.
354
	 */
355
	if (pe->pe_oh != NULL && pe->pe_oh->oh_filealign > 0)
356
		falign = pe->pe_oh->oh_filealign;
357
	else {
358
		/*
359
		 * Use the default file alignment defined by the
360
		 * PE/COFF specification.
361
		 */
362
		if (pe->pe_obj == PE_O_COFF)
363
			falign = 4;
364
		else
365
			falign = 512;
366
	}
367
368
	/*
369
	 * Step through each section (and pseduo section) and verify
370
	 * alignment constraint and overlapping, make adjustment if need.
371
	 */
372
	pe->pe_rvamax = 0;
373
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
374
		if (ps->ps_flags & LIBPE_F_STRIP_SECTION)
375
			continue;
376
377
		sh = &ps->ps_sh;
378
379
		if (sh->sh_addr + sh->sh_virtsize > pe->pe_rvamax)
380
			pe->pe_rvamax = sh->sh_addr + sh->sh_virtsize;
381
382
		if (ps->ps_ndx & 0xFFFF0000U)
383
			ps->ps_falign = 4;
384
		else
385
			ps->ps_falign = falign;
386
387
		off = roundup(off, ps->ps_falign);
388
389
		if (off != sh->sh_rawptr)
390
			ps->ps_flags |= PE_F_DIRTY;
391
392
		if (ps->ps_flags & PE_F_DIRTY) {
393
			if ((ps->ps_flags & LIBPE_F_LOAD_SECTION) == 0) {
394
				if (libpe_load_section(pe, ps) < 0)
395
					return (-1);
396
			}
397
			sh->sh_rawsize = libpe_resync_buffers(ps);
398
		}
399
400
		/*
401
		 * Sections only contains uninitialized data should set
402
		 * PointerToRawData to zero according to the PE/COFF
403
		 * specification.
404
		 */
405
		if (sh->sh_rawsize == 0)
406
			sh->sh_rawptr = 0;
407
		else
408
			sh->sh_rawptr = off;
409
410
		off += sh->sh_rawsize;
411
	}
412
413
	return (0);
414
}
415
416
off_t
417
libpe_write_section_headers(PE *pe, off_t off)
418
{
419
	char tmp[sizeof(PE_SecHdr)], *hdr;
420
	PE_Scn *ps;
421
	PE_SecHdr *sh;
422
423
	if (pe->pe_flags & LIBPE_F_BAD_SEC_HEADER || pe->pe_nscn == 0)
424
		return (off);
425
426
	if ((pe->pe_flags & LIBPE_F_DIRTY_SEC_HEADER) == 0) {
427
		off += sizeof(PE_SecHdr) * pe->pe_ch->ch_nsec;
428
		return (off);
429
	}
430
431
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
432
		if (ps->ps_flags & LIBPE_F_STRIP_SECTION)
433
			continue;
434
		if (ps->ps_ndx & 0xFFFF0000U)
435
			continue;
436
		if ((pe->pe_flags & LIBPE_F_DIRTY_SEC_HEADER) == 0 &&
437
		    (ps->ps_flags & PE_F_DIRTY) == 0)
438
			goto next_header;
439
440
		sh = &ps->ps_sh;
441
442
		memcpy(tmp, sh->sh_name, sizeof(sh->sh_name));
443
		hdr = tmp + 8;
444
		PE_WRITE32(hdr, sh->sh_virtsize);
445
		PE_WRITE32(hdr, sh->sh_addr);
446
		PE_WRITE32(hdr, sh->sh_rawsize);
447
		PE_WRITE32(hdr, sh->sh_rawptr);
448
		PE_WRITE32(hdr, sh->sh_relocptr);
449
		PE_WRITE32(hdr, sh->sh_lineptr);
450
		PE_WRITE16(hdr, sh->sh_nreloc);
451
		PE_WRITE16(hdr, sh->sh_nline);
452
		PE_WRITE32(hdr, sh->sh_char);
453
454
		if (write(pe->pe_fd, tmp, sizeof(PE_SecHdr)) !=
455
		    (ssize_t) sizeof(PE_SecHdr)) {
456
			errno = EIO;
457
			return (-1);
458
		}
459
460
	next_header:
461
		off += sizeof(PE_SecHdr);
462
	}
463
464
	return (off);
465
}
466
467
off_t
468
libpe_write_sections(PE *pe, off_t off)
469
{
470
	PE_Scn *ps;
471
	PE_SecHdr *sh;
472
473
	if (pe->pe_flags & LIBPE_F_BAD_SEC_HEADER)
474
		return (off);
475
476
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
477
		sh = &ps->ps_sh;
478
479
		if (ps->ps_flags & LIBPE_F_STRIP_SECTION)
480
			continue;
481
482
		/* Skip empty sections. */
483
		if (sh->sh_rawptr == 0 || sh->sh_rawsize == 0)
484
			continue;
485
486
		/*
487
		 * Padding between sections. (padding always written
488
		 * in case the the section headers or sections are
489
		 * moved or shrinked.)
490
		 */
491
		assert(off <= sh->sh_rawptr);
492
		if (off < sh->sh_rawptr)
493
			libpe_pad(pe, sh->sh_rawptr - off);
494
495
		if ((ps->ps_flags & PE_F_DIRTY) == 0) {
496
			assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
497
			if (lseek(pe->pe_fd,
498
			    (off_t) (sh->sh_rawptr + sh->sh_rawsize),
499
			    SEEK_SET) < 0) {
500
				errno = EIO;
501
				return (-1);
502
			}
503
			off = sh->sh_rawptr + sh->sh_rawsize;
504
			continue;
505
		}
506
507
		off = sh->sh_rawptr;
508
509
		if (libpe_write_buffers(ps) < 0)
510
			return (-1);
511
512
		off += sh->sh_rawsize;
513
514
		ps->ps_flags &= ~PE_F_DIRTY;
515
	}
516
517
	return (off);
518
}
(-)b/contrib/elftoolchain/libpe/libpe_utils.c (+69 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/param.h>
28
#include <assert.h>
29
#include <errno.h>
30
#include <string.h>
31
#include <unistd.h>
32
33
#include "_libpe.h"
34
35
ELFTC_VCSID("$Id: libpe_utils.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
36
37
off_t
38
libpe_align(PE *pe, off_t off, size_t align)
39
{
40
	off_t n;
41
42
	assert(align > 0 && (align & (align - 1)) == 0);
43
44
	n = roundup(off, align);
45
	if (n > off) {
46
		if (libpe_pad(pe, n - off) < 0)
47
			return (-1);
48
	}
49
50
	return (n);
51
}
52
53
int
54
libpe_pad(PE *pe, size_t pad)
55
{
56
	char tmp[128];
57
	size_t s;
58
59
	memset(tmp, 0, sizeof(tmp));
60
	for (; pad > 0; pad -= s) {
61
		s = pad > sizeof(tmp) ? sizeof(tmp) : pad;
62
		if (write(pe->pe_fd, tmp, s) != (ssize_t) s) {
63
			errno = EIO;
64
			return (-1);
65
		}
66
	}
67
68
	return (0);
69
}
(-)b/contrib/elftoolchain/libpe/os.Linux.mk (+6 lines)
Added Link Here
1
# $Id: os.Linux.mk 3312 2016-01-10 09:23:51Z kaiwang27 $
2
3
CFLAGS+=	-Wall -Wno-unused-parameter -Wstrict-prototypes \
4
		-Wmissing-prototypes -Wpointer-arith -Wreturn-type \
5
		-Wcast-qual -Wwrite-strings -Wswitch -Wshadow \
6
		-Wcast-align -Wunused-parameter
(-)b/contrib/elftoolchain/libpe/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/libpe/pe.h (+292 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 *
26
 * $Id: pe.h 3312 2016-01-10 09:23:51Z kaiwang27 $
27
 */
28
29
#ifndef	_PE_H_
30
#define	_PE_H_
31
32
#include <stdint.h>
33
34
/*
35
 * MS-DOS header.
36
 */
37
38
typedef struct _PE_DosHdr {
39
	char dh_magic[2];
40
	uint16_t dh_lastsize;
41
	uint16_t dh_nblock;
42
	uint16_t dh_nreloc;
43
	uint16_t dh_hdrsize;
44
	uint16_t dh_minalloc;
45
	uint16_t dh_maxalloc;
46
	uint16_t dh_ss;
47
	uint16_t dh_sp;
48
	uint16_t dh_checksum;
49
	uint16_t dh_ip;
50
	uint16_t dh_cs;
51
	uint16_t dh_relocpos;
52
	uint16_t dh_noverlay;
53
	uint16_t dh_reserved1[4];
54
	uint16_t dh_oemid;
55
	uint16_t dh_oeminfo;
56
	uint16_t dh_reserved2[10];
57
	uint32_t dh_lfanew;
58
} PE_DosHdr;
59
60
/*
61
 * Rich header.
62
 */
63
64
typedef struct _PE_RichHdr {
65
	uint32_t rh_xor;
66
	uint32_t rh_total;
67
	uint32_t *rh_compid;
68
	uint32_t *rh_cnt;
69
} PE_RichHdr;
70
71
/*
72
 * COFF header: Machine Types.
73
 */
74
75
#define	IMAGE_FILE_MACHINE_UNKNOWN	0x0	/* not specified */
76
#define	IMAGE_FILE_MACHINE_AM33		0x1d3	/* Matsushita AM33 */
77
#define	IMAGE_FILE_MACHINE_AMD64	0x8664	/* x86-64 */
78
#define	IMAGE_FILE_MACHINE_ARM		0x1c0	/* ARM LE */
79
#define	IMAGE_FILE_MACHINE_ARMNT	0x1c4	/* ARMv7(or higher) Thumb */
80
#define	IMAGE_FILE_MACHINE_ARM64	0xaa64	/* ARMv8 64-bit */
81
#define	IMAGE_FILE_MACHINE_EBC		0xebc	/* EFI byte code */
82
#define	IMAGE_FILE_MACHINE_I386		0x14c	/* x86 */
83
#define	IMAGE_FILE_MACHINE_IA64		0x200	/* IA64 */
84
#define	IMAGE_FILE_MACHINE_M32R		0x9041	/* Mitsubishi M32R LE */
85
#define	IMAGE_FILE_MACHINE_MIPS16	0x266	/* MIPS16 */
86
#define	IMAGE_FILE_MACHINE_MIPSFPU	0x366	/* MIPS with FPU */
87
#define	IMAGE_FILE_MACHINE_MIPSFPU16	0x466	/* MIPS16 with FPU */
88
#define	IMAGE_FILE_MACHINE_POWERPC	0x1f0	/* Power PC LE */
89
#define	IMAGE_FILE_MACHINE_POWERPCFP	0x1f1	/* Power PC floating point */
90
#define	IMAGE_FILE_MACHINE_R4000	0x166	/* MIPS R4000 LE */
91
#define	IMAGE_FILE_MACHINE_SH3		0x1a2	/* Hitachi SH3 */
92
#define	IMAGE_FILE_MACHINE_SH3DSP	0x1a3	/* Hitachi SH3 DSP */
93
#define	IMAGE_FILE_MACHINE_SH4		0x1a6	/* Hitachi SH4 */
94
#define	IMAGE_FILE_MACHINE_SH5		0x1a8	/* Hitachi SH5 */
95
#define	IMAGE_FILE_MACHINE_THUMB	0x1c2	/* ARM or Thumb interworking */
96
#define	IMAGE_FILE_MACHINE_WCEMIPSV2	0x169	/* MIPS LE WCE v2 */
97
98
/*
99
 * COFF header: Characteristics
100
 */
101
102
#define	IMAGE_FILE_RELOCS_STRIPPED		0x0001
103
#define	IMAGE_FILE_EXECUTABLE_IMAGE		0x0002
104
#define	IMAGE_FILE_LINE_NUMS_STRIPPED		0x0004
105
#define	IMAGE_FILE_LOCAL_SYMS_STRIPPED		0x0008
106
#define	IMAGE_FILE_AGGRESSIVE_WS_TRIM		0x0010
107
#define	IMAGE_FILE_LARGE_ADDRESS_AWARE		0x0020
108
#define	IMAGE_FILE_BYTES_REVERSED_LO		0x0080
109
#define	IMAGE_FILE_32BIT_MACHINE		0x0100
110
#define	IMAGE_FILE_DEBUG_STRIPPED		0x0200
111
#define	IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP	0x0400
112
#define	IMAGE_FILE_NET_RUN_FROM_SWAP		0x0800
113
#define	IMAGE_FILE_SYSTEM			0x1000
114
#define	IMAGE_FILE_DLL				0x2000
115
#define	IMAGE_FILE_UP_SYSTEM_ONLY		0x4000
116
#define	IMAGE_FILE_BYTES_REVERSED_HI		0x8000
117
118
/*
119
 * COFF Header.
120
 */
121
122
typedef struct _PE_CoffHdr {
123
	uint16_t ch_machine;
124
	uint16_t ch_nsec;
125
	uint32_t ch_timestamp;
126
	uint32_t ch_symptr;
127
	uint32_t ch_nsym;
128
	uint16_t ch_optsize;
129
	uint16_t ch_char;
130
} PE_CoffHdr;
131
132
133
/*
134
 * Optional Header: Subsystem.
135
 */
136
137
#define	IMAGE_SUBSYSTEM_UNKNOWN			0
138
#define	IMAGE_SUBSYSTEM_NATIVE			1
139
#define	IMAGE_SUBSYSTEM_WINDOWS_GUI		2
140
#define	IMAGE_SUBSYSTEM_WINDOWS_CUI		3
141
#define	IMAGE_SUBSYSTEM_POSIX_CUI		7
142
#define	IMAGE_SUBSYSTEM_WINDOWS_CE_GUI		9
143
#define	IMAGE_SUBSYSTEM_EFI_APPLICATION		10
144
#define	IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER	11
145
#define	IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER	12
146
#define	IMAGE_SUBSYSTEM_EFI_ROM			13
147
#define	IMAGE_SUBSYSTEM_XBOX			14
148
149
/*
150
 * Optional Header: DLL Characteristics
151
 */
152
153
#define	IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE		0x0040
154
#define	IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY	0x0080
155
#define	IMAGE_DLL_CHARACTERISTICS_NX_COMPAT		0x0100
156
#define	IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION		0x0200
157
#define	IMAGE_DLL_CHARACTERISTICS_NO_SEH		0x0400
158
#define	IMAGE_DLL_CHARACTERISTICS_NO_BIND		0x0800
159
#define	IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER		0x2000
160
#define	IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE	0x8000
161
162
/*
163
 * Optional Header.
164
 */
165
166
#define	PE_FORMAT_ROM		0x107
167
#define	PE_FORMAT_32		0x10b
168
#define	PE_FORMAT_32P		0x20b
169
170
typedef struct _PE_OptHdr {
171
	uint16_t oh_magic;
172
	uint8_t oh_ldvermajor;
173
	uint8_t oh_ldverminor;
174
	uint32_t oh_textsize;
175
	uint32_t oh_datasize;
176
	uint32_t oh_bsssize;
177
	uint32_t oh_entry;
178
	uint32_t oh_textbase;
179
	uint32_t oh_database;
180
	uint64_t oh_imgbase;
181
	uint32_t oh_secalign;
182
	uint32_t oh_filealign;
183
	uint16_t oh_osvermajor;
184
	uint16_t oh_osverminor;
185
	uint16_t oh_imgvermajor;
186
	uint16_t oh_imgverminor;
187
	uint16_t oh_subvermajor;
188
	uint16_t oh_subverminor;
189
	uint32_t oh_win32ver;
190
	uint32_t oh_imgsize;
191
	uint32_t oh_hdrsize;
192
	uint32_t oh_checksum;
193
	uint16_t oh_subsystem;
194
	uint16_t oh_dllchar;
195
	uint64_t oh_stacksizer;
196
	uint64_t oh_stacksizec;
197
	uint64_t oh_heapsizer;
198
	uint64_t oh_heapsizec;
199
	uint32_t oh_ldrflags;
200
	uint32_t oh_ndatadir;
201
} PE_OptHdr;
202
203
/*
204
 * Optional Header: Data Directories.
205
 */
206
207
#define	PE_DD_EXPORT		0
208
#define	PE_DD_IMPORT		1
209
#define	PE_DD_RESROUCE		2
210
#define	PE_DD_EXCEPTION		3
211
#define	PE_DD_CERTIFICATE	4
212
#define	PE_DD_BASERELOC		5
213
#define	PE_DD_DEBUG		6
214
#define	PE_DD_ARCH		7
215
#define	PE_DD_GLOBALPTR		8
216
#define	PE_DD_TLS		9
217
#define	PE_DD_LOADCONFIG	10
218
#define	PE_DD_BOUNDIMPORT	11
219
#define	PE_DD_IAT		12
220
#define	PE_DD_DELAYIMPORT	13
221
#define	PE_DD_CLRRUNTIME	14
222
#define	PE_DD_RESERVED		15
223
#define	PE_DD_MAX		16
224
225
typedef struct _PE_DataDirEntry {
226
	uint32_t de_addr;
227
	uint32_t de_size;
228
} PE_DataDirEntry;
229
230
typedef struct _PE_DataDir {
231
	PE_DataDirEntry dd_e[PE_DD_MAX];
232
	uint32_t dd_total;
233
} PE_DataDir;
234
235
/*
236
 * Section Headers: Section flags.
237
 */
238
239
#define	IMAGE_SCN_TYPE_NO_PAD			0x00000008
240
#define	IMAGE_SCN_CNT_CODE			0x00000020
241
#define	IMAGE_SCN_CNT_INITIALIZED_DATA		0x00000040
242
#define	IMAGE_SCN_CNT_UNINITIALIZED_DATA	0x00000080
243
#define	IMAGE_SCN_LNK_OTHER			0x00000100
244
#define	IMAGE_SCN_LNK_INFO			0x00000200
245
#define	IMAGE_SCN_LNK_REMOVE			0x00000800
246
#define	IMAGE_SCN_LNK_COMDAT			0x00001000
247
#define	IMAGE_SCN_GPREL				0x00008000
248
#define	IMAGE_SCN_MEM_PURGEABLE			0x00020000
249
#define	IMAGE_SCN_MEM_16BIT			0x00020000
250
#define	IMAGE_SCN_MEM_LOCKED			0x00040000
251
#define	IMAGE_SCN_MEM_PRELOAD			0x00080000
252
#define	IMAGE_SCN_ALIGN_1BYTES			0x00100000
253
#define	IMAGE_SCN_ALIGN_2BYTES			0x00200000
254
#define	IMAGE_SCN_ALIGN_4BYTES			0x00300000
255
#define	IMAGE_SCN_ALIGN_8BYTES			0x00400000
256
#define	IMAGE_SCN_ALIGN_16BYTES			0x00500000
257
#define	IMAGE_SCN_ALIGN_32BYTES			0x00600000
258
#define	IMAGE_SCN_ALIGN_64BYTES			0x00700000
259
#define	IMAGE_SCN_ALIGN_128BYTES		0x00800000
260
#define	IMAGE_SCN_ALIGN_256BYTES		0x00900000
261
#define	IMAGE_SCN_ALIGN_512BYTES		0x00A00000
262
#define	IMAGE_SCN_ALIGN_1024BYTES		0x00B00000
263
#define	IMAGE_SCN_ALIGN_2048BYTES		0x00C00000
264
#define	IMAGE_SCN_ALIGN_4096BYTES		0x00D00000
265
#define	IMAGE_SCN_ALIGN_8192BYTES		0x00E00000
266
#define	IMAGE_SCN_LNK_NRELOC_OVFL		0x01000000
267
#define	IMAGE_SCN_MEM_DISCARDABLE		0x02000000
268
#define	IMAGE_SCN_MEM_NOT_CACHED		0x04000000
269
#define	IMAGE_SCN_MEM_NOT_PAGED			0x08000000
270
#define	IMAGE_SCN_MEM_SHARED			0x10000000
271
#define	IMAGE_SCN_MEM_EXECUTE			0x20000000
272
#define	IMAGE_SCN_MEM_READ			0x40000000
273
#define	IMAGE_SCN_MEM_WRITE			0x80000000
274
275
/*
276
 * Section Headers.
277
 */
278
279
typedef struct _PE_SecHdr {
280
	char sh_name[8];
281
	uint32_t sh_virtsize;
282
	uint32_t sh_addr;
283
	uint32_t sh_rawsize;
284
	uint32_t sh_rawptr;
285
	uint32_t sh_relocptr;
286
	uint32_t sh_lineptr;
287
	uint16_t sh_nreloc;
288
	uint16_t sh_nline;
289
	uint32_t sh_char;
290
} PE_SecHdr;
291
292
#endif	/* !_PE_H_ */
(-)b/contrib/elftoolchain/libpe/pe_buffer.c (+100 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <errno.h>
28
29
#include "_libpe.h"
30
31
ELFTC_VCSID("$Id: pe_buffer.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
32
33
PE_Buffer *
34
pe_getbuffer(PE_Scn *ps, PE_Buffer *pb)
35
{
36
	PE *pe;
37
	PE_SecBuf *sb;
38
39
	if (ps == NULL) {
40
		errno = EINVAL;
41
		return (NULL);
42
	}
43
44
	pe = ps->ps_pe;
45
46
	if ((ps->ps_flags & LIBPE_F_LOAD_SECTION) == 0) {
47
		if (pe->pe_flags & LIBPE_F_FD_DONE) {
48
			errno = EACCES;
49
			return (NULL);
50
		}
51
		if (pe->pe_flags & LIBPE_F_SPECIAL_FILE) {
52
			if (libpe_load_all_sections(pe) < 0)
53
				return (NULL);
54
		} else {
55
			if (libpe_load_section(pe, ps) < 0)
56
				return (NULL);
57
		}
58
	}
59
60
	sb = (PE_SecBuf *) pb;
61
62
	if (sb == NULL)
63
		sb = STAILQ_FIRST(&ps->ps_b);
64
	else
65
		sb = STAILQ_NEXT(sb, sb_next);
66
67
	return ((PE_Buffer *) sb);
68
}
69
70
PE_Buffer *
71
pe_newbuffer(PE_Scn *ps)
72
{
73
	PE *pe;
74
	PE_SecBuf *sb;
75
76
	if (ps == NULL) {
77
		errno = EINVAL;
78
		return (NULL);
79
	}
80
81
	pe = ps->ps_pe;
82
83
	if (pe->pe_flags & LIBPE_F_FD_DONE) {
84
		errno = EACCES;
85
		return (NULL);
86
	}
87
88
	if ((ps->ps_flags & LIBPE_F_LOAD_SECTION) == 0) {
89
		if (libpe_load_section(pe, ps) < 0)
90
			return (NULL);
91
	}
92
93
	if ((sb = libpe_alloc_buffer(ps, 0)) == NULL)
94
		return (NULL);
95
96
	sb->sb_flags |= PE_F_DIRTY;
97
	ps->ps_flags |= PE_F_DIRTY;
98
99
	return ((PE_Buffer *) sb);
100
}
(-)b/contrib/elftoolchain/libpe/pe_cntl.c (+62 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <errno.h>
28
29
#include "_libpe.h"
30
31
ELFTC_VCSID("$Id: pe_cntl.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
32
33
int
34
pe_cntl(PE *pe, PE_Cmd cmd)
35
{
36
37
	if (pe == NULL) {
38
		errno = EINVAL;
39
		return (-1);
40
	}
41
42
	switch (cmd) {
43
	case PE_C_FDDONE:
44
		pe->pe_flags |= LIBPE_F_FD_DONE;
45
		break;
46
47
	case PE_C_FDREAD:
48
		if (pe->pe_cmd == PE_C_WRITE) {
49
			errno = EACCES;
50
			return (-1);
51
		}
52
		if (libpe_load_all_sections(pe) < 0)
53
			return (-1);
54
		break;
55
56
	default:
57
		errno = EINVAL;
58
		return (-1);
59
	}
60
61
	return (0);
62
}
(-)b/contrib/elftoolchain/libpe/pe_coff.c (+157 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <errno.h>
28
#include <stdlib.h>
29
#include <string.h>
30
31
#include "_libpe.h"
32
33
ELFTC_VCSID("$Id: pe_coff.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
34
35
PE_CoffHdr *
36
pe_coff_header(PE *pe)
37
{
38
39
	if (pe->pe_ch == NULL) {
40
		errno = ENOENT;
41
		return (NULL);
42
	}
43
44
	return (pe->pe_ch);
45
}
46
47
PE_OptHdr *
48
pe_opt_header(PE *pe)
49
{
50
51
	if (pe->pe_oh == NULL) {
52
		errno = ENOENT;
53
		return (NULL);
54
	}
55
56
	return (pe->pe_oh);
57
}
58
59
PE_DataDir *
60
pe_data_dir(PE *pe)
61
{
62
63
	if (pe->pe_dd == NULL) {
64
		errno = ENOENT;
65
		return (NULL);
66
	}
67
68
	return (pe->pe_dd);
69
}
70
71
int
72
pe_update_coff_header(PE *pe, PE_CoffHdr *ch)
73
{
74
75
	if (pe == NULL || ch == NULL) {
76
		errno = EINVAL;
77
		return (-1);
78
	}
79
80
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
81
		errno = EACCES;
82
		return (-1);
83
	}
84
85
	if (pe->pe_ch == NULL) {
86
		if ((pe->pe_ch = malloc(sizeof(PE_CoffHdr))) == NULL) {
87
			errno = ENOMEM;
88
			return (-1);
89
		}
90
	} else {
91
		/* Rewrite optional header if `optsize' field changed. */
92
		if (pe->pe_ch->ch_optsize != ch->ch_optsize)
93
			pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
94
	}
95
96
	*pe->pe_ch = *ch;
97
98
	pe->pe_flags |= LIBPE_F_DIRTY_COFF_HEADER;
99
100
	return (0);
101
}
102
103
int
104
pe_update_opt_header(PE *pe, PE_OptHdr *oh)
105
{
106
107
	if (pe == NULL || oh == NULL) {
108
		errno = EINVAL;
109
		return (-1);
110
	}
111
112
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
113
		errno = EACCES;
114
		return (-1);
115
	}
116
117
	if (pe->pe_oh == NULL) {
118
		if ((pe->pe_oh = malloc(sizeof(PE_OptHdr))) == NULL) {
119
			errno = ENOMEM;
120
			return (-1);
121
		}
122
	}
123
124
	*pe->pe_oh = *oh;
125
126
	pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
127
128
	return (0);
129
}
130
131
int
132
pe_update_data_dir(PE *pe, PE_DataDir *dd)
133
{
134
135
	if (pe == NULL || dd == NULL) {
136
		errno = EINVAL;
137
		return (-1);
138
	}
139
140
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
141
		errno = EACCES;
142
		return (-1);
143
	}
144
145
	if (pe->pe_dd == NULL) {
146
		if ((pe->pe_dd = malloc(sizeof(PE_DataDir))) == NULL) {
147
			errno = ENOMEM;
148
			return (-1);
149
		}
150
	}
151
152
	*pe->pe_dd = *dd;
153
154
	pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
155
156
	return (0);
157
}
(-)b/contrib/elftoolchain/libpe/pe_dos.c (+119 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <assert.h>
28
#include <errno.h>
29
#include <stdlib.h>
30
#include <string.h>
31
32
#include "_libpe.h"
33
34
ELFTC_VCSID("$Id: pe_dos.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
35
36
PE_DosHdr *
37
pe_msdos_header(PE *pe)
38
{
39
40
	if (pe == NULL) {
41
		errno = EINVAL;
42
		return (NULL);
43
	}
44
45
	if (pe->pe_dh == NULL) {
46
		errno = ENOENT;
47
		return (NULL);
48
	}
49
50
	return (pe->pe_dh);
51
}
52
53
char *
54
pe_msdos_stub(PE *pe, size_t *len)
55
{
56
57
	if (pe == NULL || len == NULL) {
58
		errno = EINVAL;
59
		return (NULL);
60
	}
61
62
	if (pe->pe_stub_ex > 0 &&
63
	    (pe->pe_flags & LIBPE_F_LOAD_DOS_STUB) == 0) {
64
		assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
65
		(void) libpe_read_msdos_stub(pe);
66
	}
67
68
	*len = sizeof(PE_DosHdr) + pe->pe_stub_ex;
69
70
	return (pe->pe_stub);
71
}
72
73
int
74
ps_update_msdos_header(PE *pe, PE_DosHdr *dh)
75
{
76
77
	if (pe == NULL || dh == NULL) {
78
		errno = EINVAL;
79
		return (-1);
80
	}
81
82
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
83
		errno = EACCES;
84
		return (-1);
85
	}
86
87
	if (pe->pe_dh == NULL) {
88
		if ((pe->pe_dh = malloc(sizeof(PE_DosHdr))) == NULL) {
89
			errno = ENOMEM;
90
			return (-1);
91
		}
92
	}
93
94
	*pe->pe_dh = *dh;
95
96
	pe->pe_flags |= LIBPE_F_DIRTY_DOS_HEADER;
97
98
	return (0);
99
}
100
101
int
102
ps_update_msdos_stub(PE *pe, char *dos_stub, size_t sz)
103
{
104
105
	if (pe == NULL || dos_stub == NULL || sz == 0) {
106
		errno = EINVAL;
107
		return (-1);
108
	}
109
110
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
111
		errno = EACCES;
112
		return (-1);
113
	}
114
115
	pe->pe_stub_app = dos_stub;
116
	pe->pe_stub_app_sz = sz;
117
118
	return (0);
119
}
(-)b/contrib/elftoolchain/libpe/pe_flag.c (+187 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <errno.h>
28
29
#include "_libpe.h"
30
31
ELFTC_VCSID("$Id: pe_flag.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
32
33
int
34
pe_flag(PE *pe, PE_Cmd c, unsigned int flags)
35
{
36
37
	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR)) {
38
		errno = EINVAL;
39
		return (-1);
40
	}
41
42
	if ((flags & ~(PE_F_STRIP_DOS_STUB | PE_F_STRIP_RICH_HEADER |
43
	    PE_F_STRIP_SYMTAB | PE_F_STRIP_DEBUG)) != 0) {
44
		errno = EINVAL;
45
		return (-1);
46
	}
47
48
	if (c == PE_C_SET)
49
		pe->pe_flags |= flags;
50
	else
51
		pe->pe_flags &= ~flags;
52
53
	return (0);
54
}
55
56
int
57
pe_flag_dos_header(PE *pe, PE_Cmd c, unsigned int flags)
58
{
59
60
	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
61
	    (flags & ~PE_F_DIRTY) != 0) {
62
		errno = EINVAL;
63
		return (-1);
64
	}
65
66
	if (c == PE_C_SET)
67
		pe->pe_flags |= LIBPE_F_DIRTY_DOS_HEADER;
68
	else
69
		pe->pe_flags &= ~LIBPE_F_DIRTY_DOS_HEADER;
70
71
	return (0);
72
}
73
74
int
75
pe_flag_coff_header(PE *pe, PE_Cmd c, unsigned int flags)
76
{
77
78
	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
79
	    (flags & ~PE_F_DIRTY) != 0) {
80
		errno = EINVAL;
81
		return (-1);
82
	}
83
84
	if (c == PE_C_SET)
85
		pe->pe_flags |= LIBPE_F_DIRTY_COFF_HEADER;
86
	else
87
		pe->pe_flags &= ~LIBPE_F_DIRTY_COFF_HEADER;
88
89
	return (0);
90
}
91
92
int
93
pe_flag_opt_header(PE *pe, PE_Cmd c, unsigned int flags)
94
{
95
96
	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
97
	    (flags & ~PE_F_DIRTY) != 0) {
98
		errno = EINVAL;
99
		return (-1);
100
	}
101
102
	if (c == PE_C_SET)
103
		pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
104
	else
105
		pe->pe_flags &= ~LIBPE_F_DIRTY_OPT_HEADER;
106
107
	return (0);
108
}
109
110
int
111
pe_flag_data_dir(PE *pe, PE_Cmd c, unsigned int flags)
112
{
113
114
	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
115
	    (flags & ~PE_F_DIRTY) != 0) {
116
		errno = EINVAL;
117
		return (-1);
118
	}
119
120
	if (c == PE_C_SET)
121
		pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
122
	else
123
		pe->pe_flags &= ~LIBPE_F_DIRTY_OPT_HEADER;
124
125
	return (0);
126
}
127
128
int
129
pe_flag_scn(PE_Scn *ps, PE_Cmd c, unsigned int flags)
130
{
131
132
	if (ps == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
133
	    (flags & ~(PE_F_DIRTY | PE_F_STRIP_SECTION)) == 0) {
134
		errno = EINVAL;
135
		return (-1);
136
	}
137
138
	if (c == PE_C_SET)
139
		ps->ps_flags |= flags;
140
	else
141
		ps->ps_flags &= ~flags;
142
	
143
	return (0);
144
}
145
146
int
147
pe_flag_section_header(PE_Scn *ps, PE_Cmd c, unsigned int flags)
148
{
149
	PE *pe;
150
151
	if (ps == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
152
	    (flags & ~PE_F_DIRTY) != 0) {
153
		errno = EINVAL;
154
		return (-1);
155
	}
156
157
	pe = ps->ps_pe;
158
159
	/* The library doesn't support per section header dirty flag. */
160
	if (c == PE_C_SET)
161
		pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;
162
	else
163
		pe->pe_flags &= ~LIBPE_F_DIRTY_SEC_HEADER;
164
165
	return (0);
166
}
167
168
int
169
pe_flag_buffer(PE_Buffer *pb, PE_Cmd c, unsigned int flags)
170
{
171
	PE_SecBuf *sb;
172
173
	if (pb == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
174
	    (flags & ~PE_F_DIRTY) != 0) {
175
		errno = EINVAL;
176
		return (-1);
177
	}
178
179
	sb = (PE_SecBuf *) pb;
180
181
	if (c == PE_C_SET)
182
		sb->sb_flags |= flags;
183
	else
184
		sb->sb_flags &= ~flags;
185
186
	return (0);
187
}
(-)b/contrib/elftoolchain/libpe/pe_init.c (+95 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2015 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <sys/queue.h>
28
#include <errno.h>
29
#include <stdlib.h>
30
31
#include "_libpe.h"
32
33
ELFTC_VCSID("$Id: pe_init.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
34
35
PE *
36
pe_init(int fd, PE_Cmd c, PE_Object o)
37
{
38
	PE *pe;
39
40
	if ((pe = calloc(1, sizeof(*pe))) == NULL) {
41
		errno = ENOMEM;
42
		return (NULL);
43
	}
44
	pe->pe_fd = fd;
45
	pe->pe_cmd = c;
46
	pe->pe_obj = o;
47
	STAILQ_INIT(&pe->pe_scn);
48
49
	switch (c) {
50
	case PE_C_READ:
51
	case PE_C_RDWR:
52
		if (libpe_open_object(pe) < 0)
53
			goto init_fail;
54
		break;
55
56
	case PE_C_WRITE:
57
		if (o < PE_O_PE32 || o > PE_O_COFF) {
58
			errno = EINVAL;
59
			goto init_fail;
60
		}
61
		break;
62
63
	default:
64
		errno = EINVAL;
65
		goto init_fail;
66
	}
67
68
	return (pe);
69
70
init_fail:
71
	pe_finish(pe);
72
	return (NULL);
73
}
74
75
void
76
pe_finish(PE *pe)
77
{
78
79
	if (pe == NULL)
80
		return;
81
82
	libpe_release_object(pe);
83
}
84
85
PE_Object
86
pe_object(PE *pe)
87
{
88
89
	if (pe == NULL) {
90
		errno = EINVAL;
91
		return (PE_O_UNKNOWN);
92
	}
93
94
	return (pe->pe_obj);
95
}
(-)b/contrib/elftoolchain/libpe/pe_rich.c (+107 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <assert.h>
28
#include <errno.h>
29
30
#include "_libpe.h"
31
32
ELFTC_VCSID("$Id: pe_rich.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
33
34
PE_RichHdr *
35
pe_rich_header(PE *pe)
36
{
37
38
	if (pe == NULL) {
39
		errno = EINVAL;
40
		return (NULL);
41
	}
42
43
	if (pe->pe_rh == NULL && pe->pe_stub_ex > 0 &&
44
	    (pe->pe_flags & LIBPE_F_LOAD_DOS_STUB) == 0) {
45
		assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);
46
		(void) libpe_read_msdos_stub(pe);
47
	}
48
49
	if (pe->pe_rh == NULL) {
50
		errno = ENOENT;
51
		return (NULL);
52
	}
53
54
	return (pe->pe_rh);
55
}
56
57
static uint32_t
58
rol32(uint32_t n, int c)
59
{
60
61
	c &= 0x1f;
62
63
	return ((n << c) | (n >> (0x20 - c)));
64
}
65
66
int
67
pe_rich_header_validate(PE *pe)
68
{
69
	PE_RichHdr *rh;
70
	uint32_t cksum;
71
	char *p;
72
	int i, off;
73
74
	if (pe_rich_header(pe) == NULL)
75
		return (-1);
76
77
	assert(pe->pe_rh_start != NULL);
78
79
	/*
80
	 * Initial value of the checksum is the offset to the begin of
81
	 * the Rich header.
82
	 */
83
	cksum = pe->pe_rh_start - pe->pe_stub;
84
85
	/*
86
	 * Add the bytes before the Rich header to the checksum, rotated
87
	 * left by the offset.
88
	 */
89
	for (p = pe->pe_stub; p < pe->pe_rh_start; p++) {
90
		/* Skip dh_lfanew. */
91
		off = p - pe->pe_stub;
92
		if (off >= 0x3c && off < 0x40)
93
			continue;
94
		cksum += rol32((unsigned char) *p, off);
95
	}
96
97
	/* Add each compid rotated left by its count to the checksum. */
98
	rh = pe->pe_rh;
99
	for (i = 0; (uint32_t) i < rh->rh_total; i++)
100
		cksum += rol32(rh->rh_compid[i], rh->rh_cnt[i]);
101
102
	/* Validate the checksum with the XOR mask stored after "Rich". */
103
	if (cksum == rh->rh_xor)
104
		return (1);
105
106
	return (0);
107
}
(-)b/contrib/elftoolchain/libpe/pe_section.c (+213 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <errno.h>
28
#include <string.h>
29
30
#include "_libpe.h"
31
32
ELFTC_VCSID("$Id: pe_section.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
33
34
PE_Scn *
35
pe_getscn(PE *pe, size_t ndx)
36
{
37
	PE_Scn *ps;
38
39
	if (pe == NULL || ndx < 1 || ndx > 0xFFFFU) {
40
		errno = EINVAL;
41
		return (NULL);
42
	}
43
44
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
45
		if (ps->ps_ndx == ndx)
46
			return (ps);
47
	}
48
49
	errno = ENOENT;
50
51
	return (NULL);
52
}
53
54
size_t
55
pe_ndxscn(PE_Scn *ps)
56
{
57
58
	if (ps == NULL) {
59
		errno = EINVAL;
60
		return (0);
61
	}
62
63
	return (ps->ps_ndx);
64
}
65
66
PE_Scn *
67
pe_nextscn(PE *pe, PE_Scn *ps)
68
{
69
70
	if (pe == NULL) {
71
		errno = EINVAL;
72
		return (NULL);
73
	}
74
75
	if (ps == NULL)
76
		ps = STAILQ_FIRST(&pe->pe_scn);
77
	else
78
		ps = STAILQ_NEXT(ps, ps_next);
79
80
	while (ps != NULL) {
81
		if (ps->ps_ndx >= 1 && ps->ps_ndx <= 0xFFFFU)
82
			return (ps);
83
		ps = STAILQ_NEXT(ps, ps_next);
84
	}
85
86
	return (NULL);
87
}
88
89
PE_Scn *
90
pe_newscn(PE *pe)
91
{
92
	PE_Scn *ps, *tps, *_tps;
93
94
	if (pe == NULL) {
95
		errno = EINVAL;
96
		return (NULL);
97
	}
98
99
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
100
		errno = EACCES;
101
		return (NULL);
102
	}
103
104
	if ((ps = libpe_alloc_scn(pe)) == NULL)
105
		return (NULL);
106
107
	if (pe->pe_flags & LIBPE_F_BAD_SEC_HEADER) {
108
		STAILQ_FOREACH_SAFE(tps, &pe->pe_scn, ps_next, _tps)
109
			libpe_release_scn(tps);
110
		pe->pe_flags &= ~LIBPE_F_BAD_SEC_HEADER;
111
	}
112
113
	STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);
114
115
	ps->ps_flags |= PE_F_DIRTY | LIBPE_F_LOAD_SECTION;
116
	pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;
117
118
	return (ps);
119
}
120
121
PE_Scn *
122
pe_insertscn(PE *pe, size_t ndx)
123
{
124
	PE_Scn *ps, *a, *b;
125
126
	if (pe == NULL || ndx < 1 || ndx > 0xFFFFU) {
127
		errno = EINVAL;
128
		return (NULL);
129
	}
130
131
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
132
		errno = EACCES;
133
		return (NULL);
134
	}
135
136
	if ((ps = libpe_alloc_scn(pe)) == NULL)
137
		return (NULL);
138
139
	if (pe->pe_flags & LIBPE_F_BAD_SEC_HEADER) {
140
		STAILQ_FOREACH_SAFE(a, &pe->pe_scn, ps_next, b)
141
			libpe_release_scn(a);
142
		pe->pe_flags &= ~LIBPE_F_BAD_SEC_HEADER;
143
	}
144
145
	b = NULL;
146
	STAILQ_FOREACH(a, &pe->pe_scn, ps_next) {
147
		if (a->ps_ndx & 0xFFFF0000U)
148
			continue;
149
		if (a->ps_ndx == ndx)
150
			break;
151
		b = a;
152
	}
153
154
	if (a == NULL) {
155
		STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);
156
		if (b == NULL)
157
			ps->ps_ndx = 1;
158
		else
159
			ps->ps_ndx = b->ps_ndx + 1;
160
	} else if (b == NULL) {
161
		STAILQ_INSERT_HEAD(&pe->pe_scn, ps, ps_next);
162
		ps->ps_ndx = 1;
163
	} else {
164
		STAILQ_INSERT_AFTER(&pe->pe_scn, b, ps, ps_next);
165
		ps->ps_ndx = ndx;
166
	}
167
168
	a = ps;
169
	while ((a = STAILQ_NEXT(a, ps_next)) != NULL) {
170
		if ((a->ps_ndx & 0xFFFF0000U) == 0)
171
			a->ps_ndx++;
172
	}
173
174
	ps->ps_flags |= PE_F_DIRTY | LIBPE_F_LOAD_SECTION;
175
	pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;
176
177
	return (ps);
178
}
179
180
PE_SecHdr *
181
pe_section_header(PE_Scn *ps)
182
{
183
184
	if (ps == NULL) {
185
		errno = EINVAL;
186
		return (NULL);
187
	}
188
189
	return (&ps->ps_sh);
190
}
191
192
int
193
pe_update_section_header(PE_Scn *ps, PE_SecHdr *sh)
194
{
195
	PE *pe;
196
197
	if (ps == NULL || sh == NULL) {
198
		errno = EINVAL;
199
		return (-1);
200
	}
201
202
	pe = ps->ps_pe;
203
204
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
205
		errno = EACCES;
206
		return (-1);
207
	}
208
209
	ps->ps_sh = *sh;
210
	pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;
211
212
	return (0);
213
}
(-)b/contrib/elftoolchain/libpe/pe_symtab.c (+86 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <errno.h>
28
29
#include "_libpe.h"
30
31
ELFTC_VCSID("$Id: pe_symtab.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
32
33
int
34
pe_update_symtab(PE *pe, char *symtab, size_t sz, unsigned int nsym)
35
{
36
	PE_Scn *ps;
37
	PE_SecBuf *sb;
38
	PE_SecHdr *sh;
39
40
	if (pe == NULL || symtab == NULL || sz == 0) {
41
		errno = EINVAL;
42
		return (-1);
43
	}
44
45
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
46
		errno = EACCES;
47
		return (-1);
48
	}
49
50
	/* Remove the old symbol table. */
51
	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
52
		if (ps->ps_ndx == 0xFFFFFFFFU)
53
			libpe_release_scn(ps);
54
	}
55
56
	/*
57
	 * Insert the new symbol table.
58
	 */
59
60
	if ((ps = libpe_alloc_scn(pe)) == NULL)
61
		return (-1);
62
63
	STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);
64
	ps->ps_ndx = 0xFFFFFFFFU;
65
	ps->ps_flags |= PE_F_DIRTY;
66
67
	/*
68
	 * Set the symbol table section offset to the maximum to make sure
69
	 * that it will be placed in the end of the file during section
70
	 * layout.
71
	 */
72
	sh = &ps->ps_sh;
73
	sh->sh_rawptr = 0xFFFFFFFFU;
74
	sh->sh_rawsize = sz;
75
76
	/* Allocate the buffer. */
77
	if ((sb = libpe_alloc_buffer(ps, 0)) == NULL)
78
		return (-1);
79
	sb->sb_flags |= PE_F_DIRTY;
80
	sb->sb_pb.pb_size = sz;
81
	sb->sb_pb.pb_buf = symtab;
82
83
	pe->pe_nsym = nsym;
84
85
	return (0);
86
}
(-)b/contrib/elftoolchain/libpe/pe_update.c (+86 lines)
Added Link Here
1
/*-
2
 * Copyright (c) 2016 Kai Wang
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
#include <assert.h>
28
#include <errno.h>
29
#include <unistd.h>
30
31
#include "_libpe.h"
32
33
ELFTC_VCSID("$Id: pe_update.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
34
35
off_t
36
pe_update(PE *pe)
37
{
38
	off_t off;
39
40
	if (pe == NULL) {
41
		errno = EINVAL;
42
		return (-1);
43
	}
44
45
	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
46
		errno = EACCES;
47
		return (-1);
48
	}
49
50
	if (pe->pe_cmd == PE_C_RDWR || (pe->pe_cmd == PE_C_WRITE &&
51
		(pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0)) {
52
		if (lseek(pe->pe_fd, 0, SEEK_SET) < 0) {
53
			errno = EIO;
54
			return (-1);
55
		}
56
	}
57
58
	off = 0;
59
60
	if (pe->pe_obj == PE_O_PE32 || pe->pe_obj == PE_O_PE32P) {
61
		if ((off = libpe_write_msdos_stub(pe, off)) < 0)
62
			return (-1);
63
64
		if ((off = libpe_write_pe_header(pe, off)) < 0)
65
			return (-1);
66
	}
67
68
	if (libpe_resync_sections(pe, off) < 0)
69
		return (-1);
70
71
	if ((off = libpe_write_coff_header(pe, off)) < 0)
72
		return (-1);
73
74
	if ((off = libpe_write_section_headers(pe, off)) < 0)
75
		return (-1);
76
77
	if ((off = libpe_write_sections(pe, off)) < 0)
78
		return (-1);
79
80
	if (ftruncate(pe->pe_fd, off) < 0) {
81
		errno = EIO;
82
		return (-1);
83
	}
84
85
	return (off);
86
}
(-)b/contrib/elftoolchain/nm/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/readelf/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/readelf/readelf.c (-25 / +53 lines)
Lines 47-53 Link Here
47
47
48
#include "_elftc.h"
48
#include "_elftc.h"
49
49
50
ELFTC_VCSID("$Id: readelf.c 3271 2015-12-11 18:53:08Z kaiwang27 $");
50
ELFTC_VCSID("$Id: readelf.c 3395 2016-02-10 16:29:44Z emaste $");
51
52
/* Backwards compatability for older FreeBSD releases. */
53
#ifndef	STB_GNU_UNIQUE
54
#define	STB_GNU_UNIQUE 10
55
#endif
56
#ifndef	STT_SPARC_REGISTER
57
#define	STT_SPARC_REGISTER 13
58
#endif
59
51
60
52
/*
61
/*
53
 * readelf(1) options.
62
 * readelf(1) options.
Lines 338-344 static const char *phdr_type(unsigned int ptype); Link Here
338
static const char *ppc_abi_fp(uint64_t fp);
347
static const char *ppc_abi_fp(uint64_t fp);
339
static const char *ppc_abi_vector(uint64_t vec);
348
static const char *ppc_abi_vector(uint64_t vec);
340
static const char *r_type(unsigned int mach, unsigned int type);
349
static const char *r_type(unsigned int mach, unsigned int type);
341
static void readelf_usage(void);
350
static void readelf_usage(int status);
342
static void readelf_version(void);
351
static void readelf_version(void);
343
static void search_loclist_at(struct readelf *re, Dwarf_Die die,
352
static void search_loclist_at(struct readelf *re, Dwarf_Die die,
344
    Dwarf_Unsigned lowpc);
353
    Dwarf_Unsigned lowpc);
Lines 348-354 static void set_cu_context(struct readelf *re, Dwarf_Half psize, Link Here
348
    Dwarf_Half osize, Dwarf_Half ver);
357
    Dwarf_Half osize, Dwarf_Half ver);
349
static const char *st_bind(unsigned int sbind);
358
static const char *st_bind(unsigned int sbind);
350
static const char *st_shndx(unsigned int shndx);
359
static const char *st_shndx(unsigned int shndx);
351
static const char *st_type(unsigned int stype);
360
static const char *st_type(unsigned int mach, unsigned int stype);
352
static const char *st_vis(unsigned int svis);
361
static const char *st_vis(unsigned int svis);
353
static const char *top_tag(unsigned int tag);
362
static const char *top_tag(unsigned int tag);
354
static void unload_sections(struct readelf *re);
363
static void unload_sections(struct readelf *re);
Lines 958-963 st_bind(unsigned int sbind) Link Here
958
	case STB_LOCAL: return "LOCAL";
967
	case STB_LOCAL: return "LOCAL";
959
	case STB_GLOBAL: return "GLOBAL";
968
	case STB_GLOBAL: return "GLOBAL";
960
	case STB_WEAK: return "WEAK";
969
	case STB_WEAK: return "WEAK";
970
	case STB_GNU_UNIQUE: return "UNIQUE";
961
	default:
971
	default:
962
		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
972
		if (sbind >= STB_LOOS && sbind <= STB_HIOS)
963
			return "OS";
973
			return "OS";
Lines 971-977 st_bind(unsigned int sbind) Link Here
971
}
981
}
972
982
973
static const char *
983
static const char *
974
st_type(unsigned int stype)
984
st_type(unsigned int mach, unsigned int stype)
975
{
985
{
976
	static char s_stype[32];
986
	static char s_stype[32];
977
987
Lines 987-996 st_type(unsigned int stype) Link Here
987
		if (stype >= STT_LOOS && stype <= STT_HIOS)
997
		if (stype >= STT_LOOS && stype <= STT_HIOS)
988
			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
998
			snprintf(s_stype, sizeof(s_stype), "OS+%#x",
989
			    stype - STT_LOOS);
999
			    stype - STT_LOOS);
990
		else if (stype >= STT_LOPROC && stype <= STT_HIPROC)
1000
		else if (stype >= STT_LOPROC && stype <= STT_HIPROC) {
1001
			if (mach == EM_SPARCV9 && stype == STT_SPARC_REGISTER)
1002
				return "REGISTER";
991
			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
1003
			snprintf(s_stype, sizeof(s_stype), "PROC+%#x",
992
			    stype - STT_LOPROC);
1004
			    stype - STT_LOPROC);
993
		else
1005
		} else
994
			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
1006
			snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>",
995
			    stype);
1007
			    stype);
996
		return (s_stype);
1008
		return (s_stype);
Lines 1066-1072 r_type(unsigned int mach, unsigned int type) Link Here
1066
		case 4: return "R_386_PLT32";
1078
		case 4: return "R_386_PLT32";
1067
		case 5: return "R_386_COPY";
1079
		case 5: return "R_386_COPY";
1068
		case 6: return "R_386_GLOB_DAT";
1080
		case 6: return "R_386_GLOB_DAT";
1069
		case 7: return "R_386_JMP_SLOT";
1081
		case 7: return "R_386_JUMP_SLOT";
1070
		case 8: return "R_386_RELATIVE";
1082
		case 8: return "R_386_RELATIVE";
1071
		case 9: return "R_386_GOTOFF";
1083
		case 9: return "R_386_GOTOFF";
1072
		case 10: return "R_386_GOTPC";
1084
		case 10: return "R_386_GOTPC";
Lines 1558-1564 r_type(unsigned int mach, unsigned int type) Link Here
1558
		case 4: return "R_X86_64_PLT32";
1570
		case 4: return "R_X86_64_PLT32";
1559
		case 5: return "R_X86_64_COPY";
1571
		case 5: return "R_X86_64_COPY";
1560
		case 6: return "R_X86_64_GLOB_DAT";
1572
		case 6: return "R_X86_64_GLOB_DAT";
1561
		case 7: return "R_X86_64_JMP_SLOT";
1573
		case 7: return "R_X86_64_JUMP_SLOT";
1562
		case 8: return "R_X86_64_RELATIVE";
1574
		case 8: return "R_X86_64_RELATIVE";
1563
		case 9: return "R_X86_64_GOTPCREL";
1575
		case 9: return "R_X86_64_GOTPCREL";
1564
		case 10: return "R_X86_64_32";
1576
		case 10: return "R_X86_64_32";
Lines 3465-3473 dump_symtab(struct readelf *re, int i) Link Here
3465
			continue;
3477
			continue;
3466
		}
3478
		}
3467
		printf("%6d:", j);
3479
		printf("%6d:", j);
3468
		printf(" %16.16jx", (uintmax_t)sym.st_value);
3480
		printf(" %16.16jx", (uintmax_t) sym.st_value);
3469
		printf(" %5ju", sym.st_size);
3481
		printf(" %5ju", (uintmax_t) sym.st_size);
3470
		printf(" %-7s", st_type(GELF_ST_TYPE(sym.st_info)));
3482
		printf(" %-7s", st_type(re->ehdr.e_machine,
3483
		    GELF_ST_TYPE(sym.st_info)));
3471
		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3484
		printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info)));
3472
		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3485
		printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other)));
3473
		printf(" %3s", st_shndx(sym.st_shndx));
3486
		printf(" %3s", st_shndx(sym.st_shndx));
Lines 4303-4309 dump_compatibility_tag(uint8_t *p, uint8_t *pe) Link Here
4303
	uint64_t val;
4316
	uint64_t val;
4304
4317
4305
	val = _decode_uleb128(&p, pe);
4318
	val = _decode_uleb128(&p, pe);
4306
	printf("flag = %ju, vendor = %s\n", val, p);
4319
	printf("flag = %ju, vendor = %s\n", (uintmax_t) val, p);
4307
	p += strlen((char *) p) + 1;
4320
	p += strlen((char *) p) + 1;
4308
4321
4309
	return (p);
4322
	return (p);
Lines 4997-5003 dump_dwarf_line(struct readelf *re) Link Here
4997
					break;
5010
					break;
4998
				case DW_LNS_set_isa:
5011
				case DW_LNS_set_isa:
4999
					isa = _decode_uleb128(&p, pe);
5012
					isa = _decode_uleb128(&p, pe);
5000
					printf("  Set isa to %ju\n", isa);
5013
					printf("  Set isa to %ju\n",
5014
					    (uintmax_t) isa);
5001
					break;
5015
					break;
5002
				default:
5016
				default:
5003
					/* Unrecognized extended opcodes. */
5017
					/* Unrecognized extended opcodes. */
Lines 5749-5760 dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base) Link Here
5749
			}
5763
			}
5750
			if (re->ec == ELFCLASS32)
5764
			if (re->ec == ELFCLASS32)
5751
				printf("%08jx %08jx\n",
5765
				printf("%08jx %08jx\n",
5752
				    ranges[j].dwr_addr1 + base0,
5766
				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5753
				    ranges[j].dwr_addr2 + base0);
5767
				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
5754
			else
5768
			else
5755
				printf("%016jx %016jx\n",
5769
				printf("%016jx %016jx\n",
5756
				    ranges[j].dwr_addr1 + base0,
5770
				    (uintmax_t) (ranges[j].dwr_addr1 + base0),
5757
				    ranges[j].dwr_addr2 + base0);
5771
				    (uintmax_t) (ranges[j].dwr_addr2 + base0));
5758
		}
5772
		}
5759
	}
5773
	}
5760
5774
Lines 6728-6734 dump_dwarf_loclist(struct readelf *re) Link Here
6728
		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6742
		set_cu_context(re, la->la_cu_psize, la->la_cu_osize,
6729
		    la->la_cu_ver);
6743
		    la->la_cu_ver);
6730
		for (i = 0; i < lcnt; i++) {
6744
		for (i = 0; i < lcnt; i++) {
6731
			printf("    %8.8jx ", la->la_off);
6745
			printf("    %8.8jx ", (uintmax_t) la->la_off);
6732
			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6746
			if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) {
6733
				printf("<End of list>\n");
6747
				printf("<End of list>\n");
6734
				continue;
6748
				continue;
Lines 6850-6862 hex_dump(struct readelf *re) Link Here
6850
		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6864
		if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL)
6851
			continue;
6865
			continue;
6852
		(void) elf_errno();
6866
		(void) elf_errno();
6853
		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6867
		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6868
		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
6854
			elferr = elf_errno();
6869
			elferr = elf_errno();
6855
			if (elferr != 0)
6870
			if (elferr != 0)
6856
				warnx("elf_getdata failed: %s",
6871
				warnx("elf_getdata failed: %s",
6857
				    elf_errmsg(elferr));
6872
				    elf_errmsg(elferr));
6858
			continue;
6873
			continue;
6859
		}
6874
		}
6875
		(void) elf_errno();
6860
		if (d->d_size <= 0 || d->d_buf == NULL) {
6876
		if (d->d_size <= 0 || d->d_buf == NULL) {
6861
			printf("\nSection '%s' has no data to dump.\n",
6877
			printf("\nSection '%s' has no data to dump.\n",
6862
			    s->name);
6878
			    s->name);
Lines 6905-6917 str_dump(struct readelf *re) Link Here
6905
		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6921
		if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL)
6906
			continue;
6922
			continue;
6907
		(void) elf_errno();
6923
		(void) elf_errno();
6908
		if ((d = elf_getdata(s->scn, NULL)) == NULL) {
6924
		if ((d = elf_getdata(s->scn, NULL)) == NULL &&
6925
		    (d = elf_rawdata(s->scn, NULL)) == NULL) {
6909
			elferr = elf_errno();
6926
			elferr = elf_errno();
6910
			if (elferr != 0)
6927
			if (elferr != 0)
6911
				warnx("elf_getdata failed: %s",
6928
				warnx("elf_getdata failed: %s",
6912
				    elf_errmsg(elferr));
6929
				    elf_errmsg(elferr));
6913
			continue;
6930
			continue;
6914
		}
6931
		}
6932
		(void) elf_errno();
6915
		if (d->d_size <= 0 || d->d_buf == NULL) {
6933
		if (d->d_size <= 0 || d->d_buf == NULL) {
6916
			printf("\nSection '%s' has no data to dump.\n",
6934
			printf("\nSection '%s' has no data to dump.\n",
6917
			    s->name);
6935
			    s->name);
Lines 7370-7379 _read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) Link Here
7370
	case 8:
7388
	case 8:
7371
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7389
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7372
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7390
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7391
		/* FALLTHROUGH */
7373
	case 4:
7392
	case 4:
7374
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7393
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7394
		/* FALLTHROUGH */
7375
	case 2:
7395
	case 2:
7376
		ret |= ((uint64_t) src[1]) << 8;
7396
		ret |= ((uint64_t) src[1]) << 8;
7397
		/* FALLTHROUGH */
7377
	case 1:
7398
	case 1:
7378
		ret |= src[0];
7399
		ret |= src[0];
7379
		break;
7400
		break;
Lines 7433-7442 _decode_lsb(uint8_t **data, int bytes_to_read) Link Here
7433
	case 8:
7454
	case 8:
7434
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7455
		ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40;
7435
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7456
		ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56;
7457
		/* FALLTHROUGH */
7436
	case 4:
7458
	case 4:
7437
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7459
		ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24;
7460
		/* FALLTHROUGH */
7438
	case 2:
7461
	case 2:
7439
		ret |= ((uint64_t) src[1]) << 8;
7462
		ret |= ((uint64_t) src[1]) << 8;
7463
		/* FALLTHROUGH */
7440
	case 1:
7464
	case 1:
7441
		ret |= src[0];
7465
		ret |= src[0];
7442
		break;
7466
		break;
Lines 7558-7563 Usage: %s [options] file...\n\ Link Here
7558
  -s | --syms | --symbols  Print symbol tables.\n\
7582
  -s | --syms | --symbols  Print symbol tables.\n\
7559
  -t | --section-details   Print additional information about sections.\n\
7583
  -t | --section-details   Print additional information about sections.\n\
7560
  -v | --version           Print a version identifier and exit.\n\
7584
  -v | --version           Print a version identifier and exit.\n\
7585
  -w[afilmoprsFLR] | --debug-dump={abbrev,aranges,decodedline,frames,\n\
7586
                               frames-interp,info,loc,macro,pubnames,\n\
7587
                               ranges,Ranges,rawline,str}\n\
7588
                           Display DWARF information.\n\
7561
  -x INDEX | --hex-dump=INDEX\n\
7589
  -x INDEX | --hex-dump=INDEX\n\
7562
                           Display contents of a section as hexadecimal.\n\
7590
                           Display contents of a section as hexadecimal.\n\
7563
  -A | --arch-specific     (accepted, but ignored)\n\
7591
  -A | --arch-specific     (accepted, but ignored)\n\
Lines 7574-7583 Usage: %s [options] file...\n\ Link Here
7574
7602
7575
7603
7576
static void
7604
static void
7577
readelf_usage(void)
7605
readelf_usage(int status)
7578
{
7606
{
7579
	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7607
	fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
7580
	exit(EXIT_FAILURE);
7608
	exit(status);
7581
}
7609
}
7582
7610
7583
int
7611
int
Lines 7596-7602 main(int argc, char **argv) Link Here
7596
	    longopts, NULL)) != -1) {
7624
	    longopts, NULL)) != -1) {
7597
		switch(opt) {
7625
		switch(opt) {
7598
		case '?':
7626
		case '?':
7599
			readelf_usage();
7627
			readelf_usage(EXIT_SUCCESS);
7600
			break;
7628
			break;
7601
		case 'A':
7629
		case 'A':
7602
			re->options |= RE_AA;
7630
			re->options |= RE_AA;
Lines 7621-7627 main(int argc, char **argv) Link Here
7621
			re->options |= RE_G;
7649
			re->options |= RE_G;
7622
			break;
7650
			break;
7623
		case 'H':
7651
		case 'H':
7624
			readelf_usage();
7652
			readelf_usage(EXIT_SUCCESS);
7625
			break;
7653
			break;
7626
		case 'h':
7654
		case 'h':
7627
			re->options |= RE_H;
7655
			re->options |= RE_H;
Lines 7699-7705 main(int argc, char **argv) Link Here
7699
	argc -= optind;
7727
	argc -= optind;
7700
7728
7701
	if (argc == 0 || re->options == 0)
7729
	if (argc == 0 || re->options == 0)
7702
		readelf_usage();
7730
		readelf_usage(EXIT_FAILURE);
7703
7731
7704
	if (argc > 1)
7732
	if (argc > 1)
7705
		re->flags |= DISPLAY_FILENAME;
7733
		re->flags |= DISPLAY_FILENAME;
(-)b/contrib/elftoolchain/size/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/strings/os.NetBSD.mk (+2 lines)
Added Link Here
1
# TODO(#511): Revert after the source tree is -Wconversion clean.
2
WARNS=5
(-)b/contrib/elftoolchain/strings/strings.1 (-2 / +5 lines)
Lines 22-30 Link Here
22
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
.\" SUCH DAMAGE.
23
.\" SUCH DAMAGE.
24
.\"
24
.\"
25
.\" $Id: strings.1 3195 2015-05-12 17:22:19Z emaste $
25
.\" $Id: strings.1 3360 2016-01-24 18:34:06Z jkoshy $
26
.\"
26
.\"
27
.Dd December 19, 2011
27
.Dd January 24, 2016
28
.Dt STRINGS 1
28
.Dt STRINGS 1
29
.Os
29
.Os
30
.Sh NAME
30
.Sh NAME
Lines 100-105 Print a usage summary and exit. Link Here
100
Print the contiguous character sequence of at least
100
Print the contiguous character sequence of at least
101
.Ar number
101
.Ar number
102
characters long, instead of the default of 4 characters.
102
characters long, instead of the default of 4 characters.
103
Argument
104
.Ar number
105
should specify a positive decimal integer.
103
.It Fl o
106
.It Fl o
104
Equivalent to specifying
107
Equivalent to specifying
105
.Fl t Ar o .
108
.Fl t Ar o .
(-)b/contrib/elftoolchain/strings/strings.c (-4 / +8 lines)
Lines 37-42 Link Here
37
#include <stdio.h>
37
#include <stdio.h>
38
#include <stdlib.h>
38
#include <stdlib.h>
39
#include <string.h>
39
#include <string.h>
40
#include <sysexits.h>
40
#include <unistd.h>
41
#include <unistd.h>
41
42
42
#include <libelf.h>
43
#include <libelf.h>
Lines 45-51 Link Here
45
46
46
#include "_elftc.h"
47
#include "_elftc.h"
47
48
48
ELFTC_VCSID("$Id: strings.c 3174 2015-03-27 17:13:41Z emaste $");
49
ELFTC_VCSID("$Id: strings.c 3360 2016-01-24 18:34:06Z jkoshy $");
49
50
50
enum return_code {
51
enum return_code {
51
	RETURN_OK,
52
	RETURN_OK,
Lines 73-82 enum encoding_style { Link Here
73
	  ((c) == '\t' || isprint((c)) ||			\
74
	  ((c) == '\t' || isprint((c)) ||			\
74
	      (encoding == ENCODING_8BIT && (c) > 127)))
75
	      (encoding == ENCODING_8BIT && (c) > 127)))
75
76
76
77
static int encoding_size, entire_file, show_filename, show_loc;
77
static int encoding_size, entire_file, min_len, show_filename, show_loc;
78
static enum encoding_style encoding;
78
static enum encoding_style encoding;
79
static enum radix_style radix;
79
static enum radix_style radix;
80
static intmax_t min_len;
80
81
81
static struct option strings_longopts[] = {
82
static struct option strings_longopts[] = {
82
	{ "all",		no_argument,		NULL,	'a'},
83
	{ "all",		no_argument,		NULL,	'a'},
Lines 144-150 main(int argc, char **argv) Link Here
144
			show_filename = 1;
145
			show_filename = 1;
145
			break;
146
			break;
146
		case 'n':
147
		case 'n':
147
			min_len = (int)strtoimax(optarg, (char**)NULL, 10);
148
			min_len = strtoimax(optarg, (char**)NULL, 10);
149
			if (min_len <= 0)
150
				errx(EX_USAGE, "option -n should specify a "
151
				    "positive decimal integer.");
148
			break;
152
			break;
149
		case 'o':
153
		case 'o':
150
			show_loc = 1;
154
			show_loc = 1;
(-)b/lib/Makefile (+2 lines)
Lines 80-85 SUBDIR= ${SUBDIR_ORDERED} \ Link Here
80
	libopie \
80
	libopie \
81
	libpam \
81
	libpam \
82
	libpcap \
82
	libpcap \
83
	${_libpe} \
83
	libpjdlog \
84
	libpjdlog \
84
	${_libpmc} \
85
	${_libpmc} \
85
	${_libproc} \
86
	${_libproc} \
Lines 184-189 _cuse= libcuse Link Here
184
185
185
.if ${MK_TOOLCHAIN} != "no"
186
.if ${MK_TOOLCHAIN} != "no"
186
_libelftc=	libelftc
187
_libelftc=	libelftc
188
_libpe=		libpe
187
.endif
189
.endif
188
190
189
.if ${MK_FILE} != "no"
191
.if ${MK_FILE} != "no"
(-)b/lib/libelftc/elftc_version.c (-1 / +1 lines)
Lines 6-10 Link Here
6
const char *
6
const char *
7
elftc_version(void)
7
elftc_version(void)
8
{
8
{
9
	return "elftoolchain r3272M";
9
	return "elftoolchain r3395M";
10
}
10
}
(-)b/lib/libpe/Makefile (+34 lines)
Added Link Here
1
# $FreeBSD$
2
.include <bsd.own.mk>
3
4
INTERNALLIB=
5
6
ELFTCDIR=	${.CURDIR}/../../contrib/elftoolchain
7
8
.PATH:	${ELFTCDIR}/libpe
9
10
LIB=	pe
11
12
SRCS=	libpe_buffer.c \
13
	libpe_coff.c \
14
	libpe_dos.c \
15
	libpe_init.c \
16
	libpe_rich.c \
17
	libpe_section.c \
18
	libpe_utils.c \
19
	pe_buffer.c \
20
	pe_cntl.c \
21
	pe_coff.c \
22
	pe_dos.c \
23
	pe_flag.c \
24
	pe_init.c \
25
	pe_rich.c \
26
	pe_section.c \
27
	pe_symtab.c \
28
	pe_update.c
29
30
CFLAGS+=-I${ELFTCDIR}/libpe -I${ELFTCDIR}/common
31
32
MAN=
33
34
.include <bsd.lib.mk>
(-)b/share/mk/src.libnames.mk (+4 lines)
Lines 39-44 _INTERNALLIBS= \ Link Here
39
		openbsd \
39
		openbsd \
40
		opts \
40
		opts \
41
		parse \
41
		parse \
42
		pe \
42
		readline \
43
		readline \
43
		sl \
44
		sl \
44
		sm \
45
		sm \
Lines 367-372 LDADD+= ${LDADD_${_l}} Link Here
367
LIBELFTCDIR=	${OBJTOP}/lib/libelftc
368
LIBELFTCDIR=	${OBJTOP}/lib/libelftc
368
LIBELFTC?=	${LIBELFTCDIR}/libelftc.a
369
LIBELFTC?=	${LIBELFTCDIR}/libelftc.a
369
370
371
LIBPEDIR=	${OBJTOP}/lib/libpe
372
LIBPE?=		${LIBPEDIR}/libpe.a
373
370
LIBREADLINEDIR=	${OBJTOP}/gnu/lib/libreadline/readline
374
LIBREADLINEDIR=	${OBJTOP}/gnu/lib/libreadline/readline
371
LIBREADLINE?=	${LIBREADLINEDIR}/libreadline.a
375
LIBREADLINE?=	${LIBREADLINEDIR}/libreadline.a
372
376
(-)b/usr.bin/elfcopy/Makefile (-3 / +4 lines)
Lines 17-29 CLEANFILES+= objcopy.1 Link Here
17
PROG=	elfcopy
17
PROG=	elfcopy
18
.endif
18
.endif
19
19
20
SRCS=	archive.c ascii.c binary.c main.c sections.c segments.c symbols.c
20
SRCS=	archive.c ascii.c binary.c main.c sections.c segments.c symbols.c pe.c
21
21
22
WARNS?=	5
22
WARNS?=	5
23
23
24
LIBADD=	archive elftc elf
24
LIBADD=	archive elftc elf pe
25
25
26
CFLAGS+=-I${ELFTCDIR}/libelftc -I${ELFTCDIR}/common
26
CFLAGS+=-I${ELFTCDIR}/libelftc -I${ELFTCDIR}/libpe -I${ELFTCDIR}/common
27
CFLAGS+=-DWITH_PE=1
27
28
28
MAN=	${PROG}.1 strip.1
29
MAN=	${PROG}.1 strip.1
29
30

Return to bug 207091