summaryrefslogtreecommitdiff
path: root/src/driver/pe_amain.c
blob: 286e1114c2f58b3a3993daa09e1ec937f9d65bbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2021  SysDeer Technologies, LLC        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <perk/perk.h>
#include <perk/perk_output.h>
#include "perk_driver_impl.h"
#include "perk_dprintf_impl.h"

#ifndef PERK_DRIVER_FLAGS
#define PERK_DRIVER_FLAGS	PERK_DRIVER_VERBOSITY_ERRORS \
				| PERK_DRIVER_VERBOSITY_USAGE
#endif

static const char vermsg[] = "%s%s%s (git://midipix.org/perk): "
			     "version %s%d.%d.%d%s.\n"
			     "[commit reference: %s%s%s]\n";

static const char * const pe_ver_color[6] = {
		"\x1b[1m\x1b[35m","\x1b[0m",
		"\x1b[1m\x1b[32m","\x1b[0m",
		"\x1b[1m\x1b[34m","\x1b[0m"
};

static const char * const pe_ver_plain[6] = {
		"","",
		"","",
		"",""
};

static ssize_t pe_version(struct pe_driver_ctx * dctx, int fdout)
{
	const struct pe_source_version * verinfo;
	const char * const * verclr;

	verinfo = pe_source_version();
	verclr  = isatty(fdout) ? pe_ver_color : pe_ver_plain;

	return pe_dprintf(
			fdout,vermsg,
			verclr[0],dctx->program,verclr[1],
			verclr[2],verinfo->major,verinfo->minor,
			verinfo->revision,verclr[3],
			verclr[4],verinfo->commit,verclr[5]);
}

static void pe_perform_hdrdump_actions(
	const struct pe_driver_ctx *	dctx,
	struct pe_unit_ctx *		uctx)
{
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_IMAGE_DOS_HEADER)
		pe_hdrdump_image_dos_hdr(dctx,uctx->meta);

	if (dctx->cctx->hdrdump & PERK_HDRDUMP_COFF_IMAGE_HEADER)
		pe_hdrdump_coff_image_hdr(dctx,uctx->meta);

	if (dctx->cctx->hdrdump & PERK_HDRDUMP_COFF_OBJECT_HEADER)
		pe_hdrdump_coff_object_hdr(dctx,uctx->meta);

	if (dctx->cctx->hdrdump & PERK_HDRDUMP_COFF_OPT_HEADER)
		pe_hdrdump_coff_opt_hdr(dctx,uctx->meta);

	if (dctx->cctx->hdrdump & PERK_HDRDUMP_SECTION_TABLE)
		pe_hdrdump_sec_tbl(dctx,uctx->meta);

	if (dctx->cctx->hdrdump & PERK_HDRDUMP_EXPORT_HEADER)
		pe_hdrdump_export_hdr(dctx,uctx->meta);

	if (dctx->cctx->hdrdump & PERK_HDRDUMP_IMPORT_TABLE)
		pe_hdrdump_import_tbl(dctx,uctx->meta);
}

static void pe_perform_unit_actions(
	const struct pe_driver_ctx *	dctx,
	struct pe_unit_ctx *		uctx)
{
	uint64_t flags = dctx->cctx->fmtflags;

	if (flags & PERK_OUTPUT_IMAGE_CATEGORY)
		pe_output_image_category(dctx,uctx->meta);

	if (flags & PERK_OUTPUT_IMAGE_SECTIONS)
		pe_output_image_sections(dctx,uctx->meta);

	if (flags & PERK_OUTPUT_IMAGE_SYMBOLS)
		pe_output_image_symbols(dctx,uctx->meta);

	if (flags & PERK_OUTPUT_IMAGE_STRINGS)
		pe_output_image_strings(dctx,uctx->meta);

	if (flags & PERK_OUTPUT_EXPORT_SYMS)
		pe_output_export_symbols(dctx,uctx->meta);

	if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS))
		pe_output_import_libraries(dctx,uctx->meta);

	if ((flags & PERK_OUTPUT_MDSO_LIBS) || (flags & PERK_OUTPUT_MDSO_SYMS))
		pe_output_mdso_libraries(dctx,uctx->meta);

	if (dctx->cctx->hdrdump)
		pe_perform_hdrdump_actions(dctx,uctx);
}

static int pe_exit(struct pe_driver_ctx * dctx, int ret)
{
	pe_output_error_vector(dctx);
	pe_free_driver_ctx(dctx);
	return ret;
}

int pe_main(char ** argv, char ** envp, const struct pe_fd_ctx * fdctx)
{
	int			ret;
	int			fdout;
	uint64_t		flags;
	struct pe_driver_ctx *	dctx;
	struct pe_unit_ctx *	uctx;
	const char **		unit;

	flags = PERK_DRIVER_FLAGS;
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;

	if ((ret = pe_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
		return (ret == PERK_USAGE)
			? !argv || !argv[0] || !argv[1]
			: PERK_ERROR;

	if (dctx->cctx->drvflags & PERK_DRIVER_VERSION)
		if ((pe_version(dctx,fdout)) < 0)
			return pe_exit(dctx,PERK_ERROR);

	for (unit=dctx->units; *unit; unit++) {
		if (!(pe_get_unit_ctx(dctx,*unit,&uctx))) {
			pe_perform_unit_actions(dctx,uctx);
			pe_free_unit_ctx(uctx);
		}
	}

	return pe_exit(dctx,dctx->errv[0] ? PERK_ERROR : PERK_OK);
}