/[gentoo-projects]/pax-utils/scanelf.c
Gentoo

Diff of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.25 Revision 1.26
1/* 1/*
2 * Copyright 2003 Ned Ludd <solar@gentoo.org> 2 * Copyright 2003 Ned Ludd <solar@gentoo.org>
3 * Copyright 1999-2005 Gentoo Foundation 3 * Copyright 1999-2005 Gentoo Foundation
4 * Distributed under the terms of the GNU General Public License v2 4 * Distributed under the terms of the GNU General Public License v2
5 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.25 2005/04/03 18:56:08 vapier Exp $ 5 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.26 2005/04/05 00:51:33 vapier Exp $
6 * 6 *
7 ******************************************************************** 7 ********************************************************************
8 * This program is free software; you can redistribute it and/or 8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as 9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the 10 * published by the Free Software Foundation; either version 2 of the
32#include <getopt.h> 32#include <getopt.h>
33#include <assert.h> 33#include <assert.h>
34 34
35#include "paxelf.h" 35#include "paxelf.h"
36 36
37static const char *rcsid = "$Id: scanelf.c,v 1.25 2005/04/03 18:56:08 vapier Exp $"; 37static const char *rcsid = "$Id: scanelf.c,v 1.26 2005/04/05 00:51:33 vapier Exp $";
38 38
39 39
40/* helper functions for showing errors */ 40/* helper functions for showing errors */
41#define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/ 41#define argv0 "scanelf" /*((*argv != NULL) ? argv[0] : __FILE__ "\b\b")*/
42#define warn(fmt, args...) \ 42#define warn(fmt, args...) \
76/* scan an elf file and show all the fun stuff */ 76/* scan an elf file and show all the fun stuff */
77static void scanelf_file(const char *filename) 77static void scanelf_file(const char *filename)
78{ 78{
79 int i; 79 int i;
80 char found_pax, found_stack, found_relro, found_textrel, found_rpath; 80 char found_pax, found_stack, found_relro, found_textrel, found_rpath;
81 Elf_Dyn *dyn;
82 elfobj *elf = NULL; 81 elfobj *elf;
83 82
84 found_pax = found_stack = found_relro = found_textrel = found_rpath = 0; 83 found_pax = found_stack = found_relro = found_textrel = found_rpath = 0;
85 84
86 /* verify this is real ELF */ 85 /* verify this is real ELF */
87 if ((elf = readelf(filename)) == NULL) { 86 if ((elf = readelf(filename)) == NULL) {
88 if (be_verbose > 1) printf("%s: not an ELF\n", filename); 87 if (be_verbose > 2) printf("%s: not an ELF\n", filename);
89 return; 88 return;
90 } 89 }
91 90
91 if (be_verbose > 1)
92 printf("%s: {%s,%s} scanning file\n", filename,
93 get_elfeitype(elf, EI_CLASS, elf->elf_class),
94 get_elfeitype(elf, EI_DATA, elf->data[EI_DATA]));
95 else if (be_verbose)
92 if (be_verbose) printf("%s: scanning file\n", filename); 96 printf("%s: scanning file\n", filename);
93 97
94 /* show the header */ 98 /* show the header */
95 if (!be_quiet && show_banner) { 99 if (!be_quiet && show_banner) {
96 printf(" TYPE "); 100 printf(" TYPE ");
97 if (show_pax) printf(" PAX "); 101 if (show_pax) printf(" PAX ");
102 show_banner = 0; 106 show_banner = 0;
103 } 107 }
104 108
105 /* dump all the good stuff */ 109 /* dump all the good stuff */
106 if (!be_quiet) 110 if (!be_quiet)
107 printf("%-7s ", get_elfetype(elf->ehdr->e_type)); 111 printf("%-7s ", get_elfetype(elf));
108 112
109 if (show_pax) { 113 if (show_pax) {
110 char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf)); 114 char *paxflags = pax_short_hf_flags(PAX_FLAGS(elf));
111 if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) { 115 if (!be_quiet || (be_quiet && strncmp(paxflags, "PeMRxS", 6))) {
112 found_pax = 1; 116 found_pax = 1;
114 } 118 }
115 } 119 }
116 120
117 /* stack fun */ 121 /* stack fun */
118 if (show_stack) { 122 if (show_stack) {
123#define SHOW_STACK(B) \
124 if (elf->elf_class == ELFCLASS ## B) { \
125 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
126 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
119 for (i = 0; i < elf->ehdr->e_phnum; i++) { 127 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
120 if (elf->phdr[i].p_type != PT_GNU_STACK && \ 128 if (EGET(phdr[i].p_type) != PT_GNU_STACK && \
121 elf->phdr[i].p_type != PT_GNU_RELRO) continue; 129 EGET(phdr[i].p_type) != PT_GNU_RELRO) continue; \
122
123 if (be_quiet && !(elf->phdr[i].p_flags & PF_X)) 130 if (be_quiet && !(EGET(phdr[i].p_flags) & PF_X)) \
124 continue; 131 continue; \
125
126 if (elf->phdr[i].p_type == PT_GNU_STACK) 132 if (EGET(phdr[i].p_type) == PT_GNU_STACK) \
127 found_stack = 1; 133 found_stack = 1; \
128 if (elf->phdr[i].p_type == PT_GNU_RELRO) 134 if (EGET(phdr[i].p_type) == PT_GNU_RELRO) \
129 found_relro = 1; 135 found_relro = 1; \
130
131 printf("%s ", gnu_short_stack_flags(elf->phdr[i].p_flags)); 136 printf("%s ", gnu_short_stack_flags(EGET(phdr[i].p_flags))); \
132 } 137 } \
138 }
139 SHOW_STACK(32)
140 SHOW_STACK(64)
133 if (!be_quiet && !found_stack) printf("--- "); 141 if (!be_quiet && !found_stack) printf("--- ");
134 if (!be_quiet && !found_relro) printf("--- "); 142 if (!be_quiet && !found_relro) printf("--- ");
135 } 143 }
136 144
137 /* textrel fun */ 145 /* textrel fun */
138 if (show_textrel) { 146 if (show_textrel) {
147#define SHOW_TEXTREL(B) \
148 if (elf->elf_class == ELFCLASS ## B) { \
149 Elf ## B ## _Dyn *dyn; \
150 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
151 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
139 for (i = 0; i < elf->ehdr->e_phnum; i++) { 152 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
140 if (elf->phdr[i].p_type != PT_DYNAMIC) continue; 153 if (phdr[i].p_type != PT_DYNAMIC) continue; \
141 154 dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
142 dyn = (Elf_Dyn *)(elf->data + elf->phdr[i].p_offset);
143 while (dyn->d_tag != DT_NULL) { 155 while (EGET(dyn->d_tag) != DT_NULL) { \
144 if (dyn->d_tag == DT_TEXTREL) { //dyn->d_tag != DT_FLAGS) 156 if (EGET(dyn->d_tag) == DT_TEXTREL) { /*dyn->d_tag != DT_FLAGS)*/ \
145 found_textrel = 1; 157 found_textrel = 1; \
146// if (dyn->d_un.d_val & DF_TEXTREL) 158 /*if (dyn->d_un.d_val & DF_TEXTREL)*/ \
147 printf("TEXTREL "); 159 printf("TEXTREL "); \
148 } 160 } \
149 ++dyn; 161 ++dyn; \
150 } 162 } \
151 } 163 } }
164 SHOW_TEXTREL(32)
165 SHOW_TEXTREL(64)
152 if (!be_quiet && !found_textrel) printf("------- "); 166 if (!be_quiet && !found_textrel) printf("------- ");
153 } 167 }
154 168
155 /* rpath fun */ 169 /* rpath fun */
156 /* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */ 170 /* TODO: if be_quiet, only output RPATH's which aren't in /etc/ld.so.conf */
157 if (show_rpath) { 171 if (show_rpath) {
158 char *rpath, *runpath; 172 char *rpath, *runpath;
159 Elf_Shdr *strtbl = elf_findsecbyname(elf, ".dynstr"); 173 void *strtbl_void = elf_findsecbyname(elf, ".dynstr");
160 rpath = runpath = NULL; 174 rpath = runpath = NULL;
161 175
162 if (strtbl) 176 if (strtbl_void) {
177#define SHOW_RPATH(B) \
178 if (elf->elf_class == ELFCLASS ## B) { \
179 Elf ## B ## _Dyn *dyn; \
180 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
181 Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
182 Elf ## B ## _Shdr *strtbl = SHDR ## B (strtbl_void); \
163 for (i = 0; i < elf->ehdr->e_phnum; i++) { 183 for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
164 if (elf->phdr[i].p_type != PT_DYNAMIC) continue; 184 if (EGET(phdr[i].p_type) != PT_DYNAMIC) continue; \
165 185 dyn = DYN ## B (elf->data + EGET(phdr[i].p_offset)); \
166 dyn = (Elf_Dyn *)(elf->data + elf->phdr[i].p_offset);
167 while (dyn->d_tag != DT_NULL) { 186 while (EGET(dyn->d_tag) != DT_NULL) { \
168 if (dyn->d_tag == DT_RPATH) { //|| dyn->d_tag != DT_RUNPATH) 187 if (EGET(dyn->d_tag) == DT_RPATH) { \
169 rpath = elf->data + strtbl->sh_offset + dyn->d_un.d_ptr; 188 rpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
170 found_rpath = 1; 189 found_rpath = 1; \
171 } else if (dyn->d_tag == DT_RUNPATH) { 190 } else if (EGET(dyn->d_tag) == DT_RUNPATH) { \
172 runpath = elf->data + strtbl->sh_offset + dyn->d_un.d_ptr; 191 runpath = elf->data + EGET(strtbl->sh_offset) + EGET(dyn->d_un.d_ptr); \
173 found_rpath = 1; 192 found_rpath = 1; \
174 } 193 } \
175 ++dyn; 194 ++dyn; \
176 } 195 } \
196 } }
197 SHOW_RPATH(32)
198 SHOW_RPATH(64)
177 } 199 }
178 if (rpath && runpath) { 200 if (rpath && runpath) {
179 if (!strcmp(rpath, runpath)) 201 if (!strcmp(rpath, runpath))
180 printf("%-5s ", runpath); 202 printf("%-5s ", runpath);
181 else { 203 else {

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.26

  ViewVC Help
Powered by ViewVC 1.1.20