summaryrefslogtreecommitdiff
path: root/build/tfr.c
blob: fbf9d3eca0464e8ccd392a59fc70602790891aeb (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "system.h"

#include <rpmbuild.h>
#include <argv.h>
#include <rpmfc.h>


#if HAVE_LIBELF_GELF_H

#include <libelf/gelf.h>

#if !defined(DT_GNU_PRELINKED)
#define	DT_GNU_PRELINKED	0x6ffffdf5
#endif
#if !defined(DT_GNU_LIBLIST)
#define	DT_GNU_LIBLIST		0x6ffffef9
#endif

#endif

#include "debug.h"

static int rpmfcELF(rpmfc fc)
{
#if HAVE_LIBELF_GELF_H && HAVE_LIBELF
    const char * fn = fc->fn[fc->ix];;
    Elf *elf = NULL;
    Elf_Scn *scn = NULL;
    Elf_Data *data = NULL;
    GElf_Ehdr ehdr;
    GElf_Shdr shdr;
    GElf_Dyn dyn;
    int bingo;
    int fdno;

size_t ndxscn[16];
const char * sn;

memset(ndxscn, 0, sizeof(ndxscn));
fprintf(stderr, "*** %s\n", fn);
    fdno = open(fn, O_RDONLY);
    if (fdno < 0)
	return fdno;

    (void) elf_version(EV_CURRENT);

    if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL
     || elf_kind(elf) != ELF_K_ELF
     || gelf_getehdr(elf, &ehdr) == NULL
     || !(ehdr.e_type == ET_DYN || ehdr.e_type == ET_EXEC))
	goto exit;

    bingo = 0;
    /*@-branchstate -uniondef @*/
    while (!bingo && (scn = elf_nextscn(elf, scn)) != NULL) {
	(void) gelf_getshdr(scn, &shdr);
fprintf(stderr, "\tsection %s\n", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name));
	if (shdr.sh_type >= 0 && shdr.sh_type < 16)
	    ndxscn[shdr.sh_type] = elf_ndxscn(scn);
	switch (shdr.sh_type) {
	default:
	    continue;
	    /*@switchbreak@*/ break;
	case SHT_GNU_verneed:
	    /*@switchbreak@*/ break;
	case SHT_DYNAMIC:
	    while (!bingo && (data = elf_getdata (scn, data)) != NULL) {
		int maxndx = data->d_size / shdr.sh_entsize;
		int ndx;

		for (ndx = 0; ndx < maxndx; ++ndx) {
		    (void) gelf_getdyn (data, ndx, &dyn);
		    switch (dyn.d_tag) {
		    default:
			/*@innercontinue@*/ continue;
			/*@notreached@*/ break;
		    case DT_NEEDED:
sn = elf_strptr(elf, ndxscn[SHT_STRTAB], dyn.d_un.d_val);
fprintf(stderr, "\t\tneeded %s\n", sn);
			/*@switchbreak@*/ break;
		    }
		}
	    }
	    /*@switchbreak@*/ break;
	}
    }
    /*@=branchstate =uniondef @*/

exit:
    if (elf) (void) elf_end(elf);
    return 0;
#else
    return -1;
#endif
}

static struct poptOption optionsTable[] = {

 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
	N_("Common options for all rpm modes and executables:"),
	NULL }, 

   POPT_AUTOALIAS
   POPT_AUTOHELP
   POPT_TABLEEND
};

int
main(int argc, char *const argv[])
{
    poptContext optCon;
    StringBuf sb;
    ARGV_t pav;
    int pac = 0;
    ARGV_t xav;
    ARGV_t av = NULL;
    rpmfc fc;
    int ac = 0;
    const char * s;
    int ec = 1;
    int xx;
int fcolor;

    optCon = rpmcliInit(argc, argv, optionsTable);
    if (optCon == NULL)
	goto exit;

    av = poptGetArgs(optCon);
    ac = argvCount(av);

    /* Find path to file(1). */
    s = rpmExpand("%{?__file}", NULL);
    if (!(s && *s))
	goto exit;

    /* Build argv, merging tokens from expansion with CLI args. */
    xx = poptParseArgvString(s, &pac, (const char ***)&pav);
    if (!(xx == 0 && pac > 0 && pav != NULL))
	goto exit;

    xav = NULL;
    xx = argvAppend(&xav, pav);
    xx = argvAppend(&xav, av);
    pav = _free(pav);	/* XXX popt mallocs in single blob. */
    s = _free(s);

    /* Read file(1) output. */
    sb = getOutputFrom(NULL, xav, NULL, 0, 1);
    xav = argvFree(xav);

    xx = argvSplit(&xav, getStringBuf(sb), "\n");
    sb = freeStringBuf(sb);

    xx = argvSort(xav, argvCmp);

    fc = NULL;
    xx = rpmfcClassify(&fc, xav);

    for (fc->ix = 0; fc->fn[fc->ix] != NULL; fc->ix++) {
	fcolor = fc->fcolor->vals[fc->ix];
	if (fcolor & RPMFC_ELF) {
	    xx = rpmfcELF(fc);
	    continue;
	}
    }
    fc = rpmfcFree(fc);

    xav = argvFree(xav);

exit:
    optCon = rpmcliFini(optCon);
    return ec;
}