ruby-elf 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,128 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Simple ELF parser for Ruby
3
+ #
4
+ # Copyright © 2007-2010 Diego E. "Flameeyes" Pettenò <flameeyes@gmail.com>
5
+ # Portions inspired by elf.py
6
+ # Copyright © 2002 Netgraft Corporation
7
+ # Portions inspired by elf.h
8
+ # Copyright © 1995-2006 Free Software Foundation, Inc.
9
+ #
10
+ # This program is free software; you can redistribute it and/or modify
11
+ # it under the terms of the GNU General Public License as published by
12
+ # the Free Software Foundation; either version 2 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # This program is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with this generator; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
+
24
+ module Elf
25
+ class Value
26
+ class OutOfBound < Exception
27
+ attr_reader :val
28
+
29
+ def initialize(val)
30
+ @val = val
31
+ @appendix = ""
32
+ end
33
+
34
+ def message
35
+ "Value #{@val} out of bound#{@appendix}"
36
+ end
37
+
38
+ def append_message(s)
39
+ @appendix << "\n#{s}"
40
+ end
41
+ end
42
+
43
+ def initialize(val, params)
44
+ @val = val
45
+ @mnemonic = params[0].to_s
46
+ @desc = params[1]
47
+ end
48
+
49
+ attr_reader :desc, :val, :mnemonic
50
+ alias :to_i :val
51
+ alias :to_s :desc
52
+
53
+ def ==(other)
54
+ self.class == other.class and @val == other.to_i
55
+ end
56
+
57
+ def Value.[](idx)
58
+ return @enums[idx] if @enums[idx]
59
+
60
+ # If the class has defined special ranges, handle them; a
61
+ # special range is a range of values for which unknown values
62
+ # are allowed (because they are bound to specific usage we don't
63
+ # know about — where on the other hand unknown values outside of
64
+ # these ranges are frown upon); different type of values have
65
+ # different special ranges, each with its own base name, so
66
+ # leave that to be decided by the class itself.
67
+ if self.const_defined?("SpecialRanges")
68
+ self::SpecialRanges.each_pair do |base, range|
69
+ return self::Unknown.new(idx, sprintf("%s+%07x", base, idx-range.first)) if range.include? idx
70
+ end
71
+ end
72
+
73
+ raise OutOfBound.new(idx)
74
+ end
75
+
76
+ def Value.from_string(str)
77
+ str = str.downcase
78
+
79
+ each do |value|
80
+ return value if value.mnemonic.downcase == str
81
+ end
82
+
83
+ return nil
84
+ end
85
+
86
+ def Value.has_key?(idx)
87
+ @enums.has_key?(idx)
88
+ end
89
+
90
+ def Value.fill(*hash)
91
+ if hash.size == 1 && hash[0].is_a?(Hash)
92
+ hash = hash[0]
93
+ end
94
+
95
+ @enums = { }
96
+
97
+ hash.each_pair do |index, value|
98
+ @enums[index] = self.new(index, value)
99
+ const_set(value[0], @enums[index])
100
+ end
101
+ end
102
+
103
+ def Value.each(&block)
104
+ @enums.each_value(&block)
105
+ end
106
+
107
+ private_class_method :fill
108
+
109
+ # Class for unknown values
110
+ #
111
+ # This class is used to provide a way to access at least basic
112
+ # data for values that are not known but are known valid (like OS-
113
+ # or CPU-specific types for files, sections and symbols).
114
+ #
115
+ # It mimics the basis of a Value but is custom-filled by the using
116
+ # code.
117
+ class Unknown
118
+ def initialize(val, desc)
119
+ @val = val
120
+ @desc = desc
121
+ end
122
+
123
+ attr_reader :desc, :val
124
+ alias :to_i :val
125
+ alias :to_s :desc
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,180 @@
1
+ '\" t
2
+ .\" Title: cowstats
3
+ .\" Author:
4
+ .\" Generator: DocBook XSL-NS Stylesheets v1.76.0 <http://docbook.sf.net/>
5
+ .\" Date: October 2008
6
+ .\" Manual: Reference
7
+ .\" Source: ruby-elf
8
+ .\" Language: English
9
+ .\"
10
+ .TH "COWSTATS" "1" "October 2008" "ruby-elf" "Reference"
11
+ .\" -----------------------------------------------------------------
12
+ .\" * Define some portability stuff
13
+ .\" -----------------------------------------------------------------
14
+ .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
+ .\" http://bugs.debian.org/507673
16
+ .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17
+ .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
+ .ie \n(.g .ds Aq \(aq
19
+ .el .ds Aq '
20
+ .\" -----------------------------------------------------------------
21
+ .\" * set default formatting
22
+ .\" -----------------------------------------------------------------
23
+ .\" disable hyphenation
24
+ .nh
25
+ .\" disable justification (adjust text to left margin only)
26
+ .ad l
27
+ .\" -----------------------------------------------------------------
28
+ .\" * MAIN CONTENT STARTS HERE *
29
+ .\" -----------------------------------------------------------------
30
+ .SH "NAME"
31
+ cowstats \- ELF Copy\-on\-Write analyzer
32
+ .SH "SYNOPSIS"
33
+ .HP \w'\fBcowstats\fR\ 'u
34
+ \fBcowstats\fR [\fB\-\-statistics\fR] [\fB\-\-total\fR] [\fB\-\-ignore\-cxx\fR] [\fB\-\-ignore\-profiling\fR] [\fB\-\-ignore\-data\-rel\-ro\fR] [\fB\-\-sort\-by\fR\ \fIsection\-column\fR] [\fB\-\-quiet\fR] [\fB\-\-recursive\fR] [\fB@\fR\fIfile\fR | \fIfile\fR...]
35
+ .SH "DESCRIPTION"
36
+ .PP
37
+
38
+ \fBcowstats\fR
39
+ is a script that analyses ELF object files, results of compilation of C, C++ or other languages on an Unix system, and reports about the variables that enter Copy\-on\-Write sections\&.
40
+ .PP
41
+ Static variables (initialised and not) and constant pointers on PIC or PIE enabled object files are emitted in the so\-called Copy\-on\-Write sections, which require copying over pages from the original ELF executable file to a private resident area of memory at runtime\&.
42
+ .PP
43
+
44
+ \fBcowstats\fR
45
+ reports the possible symbols that were emitted in Copy\-on\-Write sections so that they can be looked after to see if they can be made constant and/or removed or reworked\&.
46
+ .SH "OPTIONS"
47
+ .PP
48
+ \fB\-s\fR, \fB\-\-statistics\fR
49
+ .RS 4
50
+ Instead of reporting all the variables found in Copy\-on\-Write sections, only generate a table showing the sie of data in Copy\-on\-Write sections per each file, divided into
51
+ \fB\&.data\fR,
52
+ \fB\&.bss\fR
53
+ and
54
+ \fB\&.data\&.rel\fR
55
+ (for variables, uninitialised variables, and relocated variables and constants)\&.
56
+ .RE
57
+ .PP
58
+ \fB\-t\fR, \fB\-\-total\fR
59
+ .RS 4
60
+ Shows some rough totals for the amount of data in Copy\-on\-Write sections for the program, assuming all the object files given are linked in the same executable\&. This will also show a rough page\-based total, which bases itself on 4K\-sized pages\&.
61
+ .RE
62
+ .PP
63
+ \fB\-x\fR, \fB\-\-ignore\-cxx\fR
64
+ .RS 4
65
+ Ignore some C++ entries that could be considered false positives\&. C++ object files will report as CoW data the vtables and typeinfo objects for C++ classes, since they are actually emitted in Copy\-on\-Write sections\&. Since they cannot be moved from thre, this option hides them on the output, to reduce clutter and noise\&.
66
+ .RE
67
+ .PP
68
+ \fB\-p\fR, \fB\-\-ignore\-profiling\fR
69
+ .RS 4
70
+ Similarly to C++, also profiling (with
71
+ \fBgcov\fR) will add some symbols that would be identified as CoW data\&. Use this option to avoid reporting those symbols\&.
72
+ .RE
73
+ .PP
74
+ \fB\-r\fR, \fB\-\-ignore\-data\-rel\-ro\fR
75
+ .RS 4
76
+ Don\*(Aqt report constants found in the \&.data\&.rel\&.ro section, and consider it as non\-relocated\&. This is helpful to reduce the noise when looking for writable data symbols, or when analysing non\-PIC builds\&.
77
+ .RE
78
+ .PP
79
+ \fB\-S\fR \fIsection\-column\fR, \fB\-\-sort\-by\fR \fIsection\-column\fR
80
+ .RS 4
81
+ Sort the output of
82
+ \fB\-\-statistics\fR
83
+ by the given column\&. Useful when looking for which objects have the most hit for one particular CoW problem\&. The column can be one of the following section names:
84
+ .sp
85
+ .RS 4
86
+ .ie n \{\
87
+ \h'-04'\(bu\h'+03'\c
88
+ .\}
89
+ .el \{\
90
+ .sp -1
91
+ .IP \(bu 2.3
92
+ .\}
93
+ \&.bss
94
+ .RE
95
+ .sp
96
+ .RS 4
97
+ .ie n \{\
98
+ \h'-04'\(bu\h'+03'\c
99
+ .\}
100
+ .el \{\
101
+ .sp -1
102
+ .IP \(bu 2.3
103
+ .\}
104
+ \&.data
105
+ .RE
106
+ .sp
107
+ .RS 4
108
+ .ie n \{\
109
+ \h'-04'\(bu\h'+03'\c
110
+ .\}
111
+ .el \{\
112
+ .sp -1
113
+ .IP \(bu 2.3
114
+ .\}
115
+ \&.data\&.rel
116
+ .RE
117
+ .sp
118
+ .RS 4
119
+ .ie n \{\
120
+ \h'-04'\(bu\h'+03'\c
121
+ .\}
122
+ .el \{\
123
+ .sp -1
124
+ .IP \(bu 2.3
125
+ .\}
126
+ \&.data\&.rel\&.ro
127
+ .RE
128
+ .RE
129
+ .PP
130
+ \fB\-q\fR, \fB\-\-quiet\fR
131
+ .RS 4
132
+ Do not output warnings and errors to the standard error\&. Designed to increase the signal\-to\-noise ratio when analysing eterogeneous trees recursively, or when producing output to redirect to automated systems\&.
133
+ .RE
134
+ .PP
135
+ \fB\-R\fR, \fB\-\-recursive\fR
136
+ .RS 4
137
+ Recursively descend into directories to search for files to scan\&. This affects both the paths passed from the command line and those found in argument files\&.
138
+ .RE
139
+ .PP
140
+ \fB@\fR\fIpath\fR
141
+ .RS 4
142
+ Read the list of files to analyse from the given file (or standard input if
143
+ \fIpath\fR
144
+ is
145
+ \-)\&. Useful to pass a long list of files\&. When used with stdin or named pipes, this also allows asynchronous analysis\&.
146
+ .RE
147
+ .SH "BUGS"
148
+ .PP
149
+
150
+ \fBcowstats\fR
151
+ is still an experiment, and is not yet entirely complete, there are thus a number of bugs that haven\*(Aqt been discovered or well tested yet\&.
152
+ .PP
153
+ A known "bug" or misbehaviour is that
154
+ \fBcowstats\fR
155
+ cannot know whether multple object files will be linked together in the same module (executable or shared object) or not\&. For this reason the output of
156
+ \fB\-\-total\fR
157
+ might not be consistent with the runtime behaviour of the module itself\&.
158
+ .PP
159
+ Parsing of files to provide further arguments (\fB@\fR\fIfile\fR) is not entirely comforming to other tools handling of the same syntax\&. No options are parsed from the file, and filenames are expected to be separated by newlines rather than whitespace\&.
160
+ .PP
161
+ Symbolic links are only followed when they are passed directly to the command line, or through @\-lists; symbolic links are
162
+ \fInot\fR
163
+ followed when using the
164
+ \fB\-\-recursive\fR
165
+ option, to avoid loops\&.
166
+ .SH "SEE ALSO"
167
+ .PP
168
+
169
+ \m[blue]\fBFlameeyes\*(Aqs Weblog\fR\m[]
170
+ http://blog\&.flameeyes\&.eu/
171
+ .PP
172
+ Related tools:
173
+ \fBrbelf-size\fR(1),
174
+ \fBobjdump\fR(1)\&.
175
+ .SH "AUTHOR"
176
+ .PP
177
+ \fBDiego E. Pettenò\fR <\&flameeyes@gmail.com\&>
178
+ .RS 4
179
+ Author and main contributor.
180
+ .RE
@@ -0,0 +1,188 @@
1
+ '\" t
2
+ .\" Title: elfgrep
3
+ .\" Author:
4
+ .\" Generator: DocBook XSL-NS Stylesheets v1.76.0 <http://docbook.sf.net/>
5
+ .\" Date: January 2011
6
+ .\" Manual: Reference
7
+ .\" Source: ruby-elf
8
+ .\" Language: English
9
+ .\"
10
+ .TH "ELFGREP" "1" "January 2011" "ruby-elf" "Reference"
11
+ .\" -----------------------------------------------------------------
12
+ .\" * Define some portability stuff
13
+ .\" -----------------------------------------------------------------
14
+ .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
+ .\" http://bugs.debian.org/507673
16
+ .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17
+ .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
+ .ie \n(.g .ds Aq \(aq
19
+ .el .ds Aq '
20
+ .\" -----------------------------------------------------------------
21
+ .\" * set default formatting
22
+ .\" -----------------------------------------------------------------
23
+ .\" disable hyphenation
24
+ .nh
25
+ .\" disable justification (adjust text to left margin only)
26
+ .ad l
27
+ .\" -----------------------------------------------------------------
28
+ .\" * MAIN CONTENT STARTS HERE *
29
+ .\" -----------------------------------------------------------------
30
+ .SH "NAME"
31
+ elfgrep \- Search for symbols matching an expression in ELF files
32
+ .SH "SYNOPSIS"
33
+ .HP \w'\fBelfgrep\fR\ 'u
34
+ \fBelfgrep\fR [\fB\-\-fixed\-strings\fR] {\fB\-\-regexp\fR\ \fIPATTERN\fR...} [\fB\-\-ignore\-case\fR] [\fB\-\-match\-version\fR] [\fB\-\-no\-match\-undefined\fR | \fB\-\-no\-match\-defined\fR] [\fB\-\-invert\-match\fR] [\fB\-\-count\fR] [\fB\-\-files\-without\-match\fR | \fB\-\-files\-with\-matches\fR] [\fB\-\-with\-filename\fR | \fB\-\-no\-filename\fR] [\fB\-\-null\fR] [\fB\-\-quiet\fR] [\fB\-\-recursive\fR] [\fB@\fR\fIfile\fR | \fIfile\fR...]
35
+ .SH "DESCRIPTION"
36
+ .PP
37
+
38
+ \fBelfgrep\fR
39
+ is a simple script that allows to earch for particular symbols within a file, by matching regular expression on their name\&. It is insipired by the common Unix
40
+ \fBgrep\fR(1)
41
+ tool\&.
42
+ .SH "OPTIONS"
43
+ .SS "Matching Control"
44
+ .PP
45
+ \fB\-F\fR, \fB\-\-fixed\-strings\fR
46
+ .RS 4
47
+ Interpret
48
+ \fIPATTERN\fR
49
+ as a fixed string\&.
50
+ .RE
51
+ .PP
52
+ \fB\-e\fR \fIPATTERN\fR, \fB\-\-regexp\fR \fIPATTERN\fR
53
+ .RS 4
54
+ Specifies the expression to match on the symbols\&. In contrast to regular
55
+ \fBgrep\fR(1)
56
+ you
57
+ \fIhave\fR
58
+ to provide at least an expression through the
59
+ \fB\-\-regexp\fR
60
+ option\&.
61
+ .RE
62
+ .PP
63
+ \fB\-i\fR, \fB\-\-ignore\-case\fR
64
+ .RS 4
65
+ Ignore case distinction in both the
66
+ \fIPATTERN\fR
67
+ and the symbols\*(Aq names\&.
68
+ .RE
69
+ .PP
70
+ \fB\-V\fR, \fB\-\-match\-version\fR
71
+ .RS 4
72
+ Append the ELF version information for the symbol, separated by an @ symbol, before testing the expression for match\&. This allows to match only symbols that are defined with a particular version\&.
73
+ .RE
74
+ .PP
75
+ \fB\-U\fR, \fB\-\-no\-match\-undefined\fR
76
+ .RS 4
77
+ Do not report matches on undefined symbols; useful if you\*(Aqre looking for the objects defining the symbol, and not those using it\&.
78
+ .RE
79
+ .PP
80
+ \fB\-D\fR, \fB\-\-no\-match\-defined\fR
81
+ .RS 4
82
+ Do not report matches on defined symbols; useful if you\*(Aqre looking for the objects using the symbol, and not those defining it\&.
83
+ .RE
84
+ .PP
85
+ \fB\-v\fR, \fB\-\-invert\-match\fR
86
+ .RS 4
87
+ Invert the sense of matching, to select non\-matching symbols\&. This does not invert the sense of
88
+ \fB\-\-no\-match\-undefined\fR
89
+ and
90
+ \fB\-\-no\-match\-defined\fR\&.
91
+ .RE
92
+ .SS "Output Control"
93
+ .PP
94
+ \fB\-c\fR, \fB\-\-count\fR
95
+ .RS 4
96
+ Suppress normal output; instead print a count of matching lines for each input file\&. With the
97
+ \fB\-\-invert\-match\fR
98
+ option, count non\-matching lines\&.
99
+ .RE
100
+ .PP
101
+ \fB\-L\fR, \fB\-\-files\-without\-match\fR
102
+ .RS 4
103
+ Suppress normal output; instead print the name of each input file from which no output would normally have been printed\&.
104
+ .RE
105
+ .PP
106
+ \fB\-l\fR, \fB\-\-files\-with\-matches\fR
107
+ .RS 4
108
+ Suppress normal output; instead print the name of each input file from which output would normally have been printed\&. The scalling will stop on the first match\&.
109
+ .RE
110
+ .PP
111
+ \fB\-H\fR, \fB\-\-with\-filename\fR
112
+ .RS 4
113
+ Print the file name for each match\&. This is the default when there is more than one file to search\&.
114
+ .RE
115
+ .PP
116
+ \fB\-h\fR, \fB\-\-no\-filename\fR
117
+ .RS 4
118
+ Suppress the prefixing of file names on output\&. This is the default when there is only one file to search\&.
119
+ .RE
120
+ .PP
121
+ \fB\-Z\fR, \fB\-\-null\fR
122
+ .RS 4
123
+ Output a zero byte (the ASCII
124
+ NUL
125
+ character) instead of the character that normally follows a file name\&. For example
126
+ \fBelfgrep \-lZ\fR
127
+ outputs a zero byte after each file name instead of the usual newline\&. This option makes the output unambiguous, even in presence of file names containing unusual characters like newlines, so that it can be used with commands like
128
+ \fBxargs \-0\fR\&.
129
+ .RE
130
+ .SS "General Options"
131
+ .PP
132
+ \fB\-q\fR, \fB\-\-quiet\fR
133
+ .RS 4
134
+ Do not output warnings and errors to the standard error\&. Designed to increase the signal\-to\-noise ratio when analysing eterogeneous trees recursively, or when producing output to redirect to automated systems\&.
135
+ .RE
136
+ .PP
137
+ \fB\-R\fR, \fB\-\-recursive\fR
138
+ .RS 4
139
+ Recursively descend into directories to search for files to scan\&. This affects both the paths passed from the command line and those found in argument files\&.
140
+ .RE
141
+ .PP
142
+ \fB@\fR\fIpath\fR
143
+ .RS 4
144
+ Read the list of files to analyse from the given file (or standard input if
145
+ \fIpath\fR
146
+ is
147
+ \-)\&. Useful to pass a long list of files\&. When used with stdin or named pipes, this also allows asynchronous analysis\&.
148
+ .RE
149
+ .SH "BUGS AND MISSING FEATURES"
150
+ .PP
151
+ By default,
152
+ elfgrep
153
+ uses standard Ruby regular expressions, which are neither the basic or extended regular expressions as implemented by
154
+ \fBgrep\fR(1)
155
+ nor the Perl (or compatible) regular expressions\&.
156
+ .PP
157
+ The
158
+ \fB\-\-fixed\-strings\fR
159
+ option does not conform completely with the equivalent option from
160
+ \fBgrep\fR(1)
161
+ as it doesn\*(Aqt take a newline\-separated list of strings, but only a single string\&.
162
+ .PP
163
+ Parsing of files to provide further arguments (\fB@\fR\fIfile\fR) is not entirely comforming to other tools handling of the same syntax\&. No options are parsed from the file, and filenames are expected to be separated by newlines rather than whitespace\&.
164
+ .PP
165
+ Symbolic links are only followed when they are passed directly to the command line, or through @\-lists; symbolic links are
166
+ \fInot\fR
167
+ followed when using the
168
+ \fB\-\-recursive\fR
169
+ option, to avoid loops\&.
170
+ .SH "SEE ALSO"
171
+ .PP
172
+
173
+ \m[blue]\fBFlameeyes\*(Aqs Weblog\fR\m[]
174
+ http://blog\&.flameeyes\&.eu/
175
+ .PP
176
+ Related tools:
177
+ \fBgrep\fR(1),
178
+ \fBnm\fR(1)\&.
179
+ .PP
180
+ Lots of description of options above are lifted directly from the
181
+ grep
182
+ man page, to avoid confusing with different wordings\&.
183
+ .SH "AUTHOR"
184
+ .PP
185
+ \fBDiego E. Pettenò\fR <\&flameeyes@gmail.com\&>
186
+ .RS 4
187
+ Author and main contributor.
188
+ .RE