elf_utils 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.standard.yml +3 -0
  4. data/CODE_OF_CONDUCT.md +132 -0
  5. data/CONTRIBUTING.md +55 -0
  6. data/Gemfile +23 -0
  7. data/LICENSE.txt +21 -0
  8. data/MAINTAINERS.md +3 -0
  9. data/README.md +126 -0
  10. data/Rakefile +76 -0
  11. data/SECURITY.md +57 -0
  12. data/elf_utils.gemspec +41 -0
  13. data/ext/elf_utils/elf_utils.c +53 -0
  14. data/ext/elf_utils/extconf.rb +3 -0
  15. data/lib/elf_utils/elf_file.rb +312 -0
  16. data/lib/elf_utils/section/base.rb +77 -0
  17. data/lib/elf_utils/section/debug_abbrev/abbreviation.rb +171 -0
  18. data/lib/elf_utils/section/debug_abbrev/abbreviation_table.rb +27 -0
  19. data/lib/elf_utils/section/debug_abbrev.rb +15 -0
  20. data/lib/elf_utils/section/debug_addr.rb +9 -0
  21. data/lib/elf_utils/section/debug_arange.rb +54 -0
  22. data/lib/elf_utils/section/debug_info/compilation_unit.rb +189 -0
  23. data/lib/elf_utils/section/debug_info/debug_str_offsets_ref.rb +15 -0
  24. data/lib/elf_utils/section/debug_info/debug_str_ref.rb +17 -0
  25. data/lib/elf_utils/section/debug_info/die/base.rb +130 -0
  26. data/lib/elf_utils/section/debug_info/die.rb +470 -0
  27. data/lib/elf_utils/section/debug_info/die_ref.rb +22 -0
  28. data/lib/elf_utils/section/debug_info/header.rb +26 -0
  29. data/lib/elf_utils/section/debug_info.rb +93 -0
  30. data/lib/elf_utils/section/debug_line/line_number_program/header.rb +48 -0
  31. data/lib/elf_utils/section/debug_line/line_number_program/state_machine.rb +206 -0
  32. data/lib/elf_utils/section/debug_line/line_number_program.rb +134 -0
  33. data/lib/elf_utils/section/debug_line.rb +35 -0
  34. data/lib/elf_utils/section/debug_ranges.rb +22 -0
  35. data/lib/elf_utils/section/debug_str_offsets.rb +16 -0
  36. data/lib/elf_utils/section/dynsym.rb +14 -0
  37. data/lib/elf_utils/section/strtab.rb +9 -0
  38. data/lib/elf_utils/section/symtab.rb +11 -0
  39. data/lib/elf_utils/section.rb +50 -0
  40. data/lib/elf_utils/segment/base.rb +72 -0
  41. data/lib/elf_utils/segment.rb +9 -0
  42. data/lib/elf_utils/string_pread.rb +18 -0
  43. data/lib/elf_utils/symbol.rb +144 -0
  44. data/lib/elf_utils/types/dwarf/expression.rb +34 -0
  45. data/lib/elf_utils/types/dwarf.rb +639 -0
  46. data/lib/elf_utils/types/dwarf32/v2.rb +44 -0
  47. data/lib/elf_utils/types/dwarf32/v3.rb +40 -0
  48. data/lib/elf_utils/types/dwarf32/v4.rb +41 -0
  49. data/lib/elf_utils/types/dwarf32/v5.rb +44 -0
  50. data/lib/elf_utils/types/dwarf32.rb +12 -0
  51. data/lib/elf_utils/types/dwarf64/v3.rb +42 -0
  52. data/lib/elf_utils/types/dwarf64/v4.rb +43 -0
  53. data/lib/elf_utils/types/dwarf64/v5.rb +46 -0
  54. data/lib/elf_utils/types/dwarf64.rb +8 -0
  55. data/lib/elf_utils/types/sleb128.rb +66 -0
  56. data/lib/elf_utils/types/uleb128.rb +56 -0
  57. data/lib/elf_utils/types/unit_length.rb +51 -0
  58. data/lib/elf_utils/types.rb +328 -0
  59. data/lib/elf_utils/version.rb +5 -0
  60. data/lib/elf_utils.rb +83 -0
  61. data/sig/elf_utils.rbs +4 -0
  62. metadata +120 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dbf65536445ceb5a50fd33bb13227ee00837dde499a258a7e9004b3fc9a9716b
4
+ data.tar.gz: bc21c9017ecabe98263976c14cde0469a1da4c4bf1abca148d96b7eafdb31541
5
+ SHA512:
6
+ metadata.gz: 5c976e5b1da62c450aff9448c6b4c5b3984819538d6973179a594cfdf1209805821530d9b7fe61389f064287a7dab28e33635306791e895eae005efe6d279d0b
7
+ data.tar.gz: c0db80808986319a171ca5f0452369668baa9cef8085fe38e9a76957dd54d79edf1960fd4d9fc7b2100d7793ee7b501150cf5c7b3fb700c1f2ac2a19b3f93894
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 3.1
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ `dlary[at]cisco<dot>com`. All complaints will be reviewed and investigated
64
+ promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
+ enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,55 @@
1
+ # How to Contribute
2
+
3
+ Thanks for your interest in contributing to `elf_utils`! Here are a few
4
+ general guidelines on contributing and reporting bugs that we ask you to review.
5
+ Following these guidelines helps to communicate that you respect the time of the
6
+ contributors managing and developing this open source project. In return, they
7
+ should reciprocate that respect in addressing your issue, assessing changes, and
8
+ helping you finalize your pull requests. In that spirit of mutual respect, we
9
+ endeavor to review incoming issues and pull requests within 10 days, and will
10
+ close any lingering issues or pull requests after 60 days of inactivity.
11
+
12
+ Please note that all of your interactions in the project are subject to our
13
+ [Code of Conduct](/CODE_OF_CONDUCT.md). This includes creation of issues or pull
14
+ requests, commenting on issues or pull requests, and extends to all interactions
15
+ in any real-time space e.g., Slack, Discord, etc.
16
+
17
+ ## Reporting Issues
18
+
19
+ Before reporting a new issue, please ensure that the issue was not already
20
+ reported or fixed by searching through our
21
+ [issues list](https://github.com/cisco-open/elf_utils/issues).
22
+
23
+ When creating a new issue, please be sure to include a **title and clear
24
+ description**, as much relevant information as possible, and, if possible, a
25
+ test case.
26
+
27
+ **If you discover a security bug, please do not report it through GitHub.
28
+ Instead, please see security procedures in [SECURITY.md](/SECURITY.md).**
29
+
30
+ ## Sending Pull Requests
31
+
32
+ Before sending a new pull request, take a look at existing pull requests and
33
+ issues to see if the proposed change or fix has been discussed in the past, or
34
+ if the change was already implemented but not yet released.
35
+
36
+ We expect new pull requests to include tests for any affected behavior, and,
37
+ as we follow semantic versioning, we may reserve breaking changes until the
38
+ next major version release.
39
+
40
+ ## Other Ways to Contribute
41
+
42
+ We welcome anyone that wants to contribute to `elf_utils` to triage and
43
+ reply to open issues to help troubleshoot and fix existing bugs. Here is what
44
+ you can do:
45
+
46
+ - Help ensure that existing issues follows the recommendations from the
47
+ _[Reporting Issues](#reporting-issues)_ section, providing feedback to the
48
+ issue's author on what might be missing.
49
+ - Review existing pull requests, and testing patches against real existing
50
+ applications that use `elf_utils`.
51
+ - Write a test, or add a missing test case to an existing test.
52
+
53
+ Thanks again for your interest on contributing to `elf_utils`!
54
+
55
+ :heart:
data/Gemfile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in elfutils.gemspec
6
+ gemspec
7
+
8
+ group :test do
9
+ gem "jsonpath", "~> 1.1"
10
+ gem "pry", "~> 0.14"
11
+ gem "pry-rescue", "~> 1.5"
12
+ gem "pry-stack_explorer", "~> 0.6"
13
+ gem "rspec", "~> 3.0"
14
+ end
15
+
16
+ group :development do
17
+ gem "rake", "~> 13.0"
18
+ gem "standard", "~> 1.3"
19
+ gem "ruby-prof", "~> 1.6"
20
+ gem "benchmark", "~> 0.2.1"
21
+ gem "benchmark-ips", "~> 2.12"
22
+ gem "rake-compiler", "~> 1.2"
23
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Cisco
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/MAINTAINERS.md ADDED
@@ -0,0 +1,3 @@
1
+ # Maintainers
2
+
3
+ - [dmlary](https://github.com/dmlary)
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # ElfUtils
2
+
3
+ [![Version](https://img.shields.io/gem/v/elf_utils.svg)](https://rubygems.org/gems/elf_utils)
4
+ [![GitHub](https://img.shields.io/badge/github-elf__utils-blue.svg)](http://github.com/cisco-open/ruby-elf_utils)
5
+ [![Documentation](https://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://rubydoc.org/gems/ruby-elf_utils/frames)
6
+
7
+ [![Contributor-Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-fbab2c.svg)](CODE_OF_CONDUCT.md)
8
+ [![Maintainer](https://img.shields.io/badge/Maintainer-Cisco-00bceb.svg)](https://opensource.cisco.com)
9
+
10
+ ElfUtils is a Ruby gem for parsing ELF files and DWARF debugging information.
11
+
12
+ ElfUtils supports 32-bit & 64-bit ELF format, both big and little endian. It
13
+ also supports DWARF versions 2, 3, 4, and 5. The handling of both ELF and
14
+ DWARF formats **is not currently exhaustive**, but is sufficient to perform
15
+ many common tasks such as calculating symbol address, finding symbol source
16
+ location, extracting variable data types.
17
+
18
+ If you encounter a place where the format support is incomplete, please open
19
+ an issue. We test against the generated binaries in [spec/data/](spec/data),
20
+ but need more real-world examples to ensure proper format coverage.
21
+
22
+ ## Installation
23
+
24
+ Install the gem and add to the application's Gemfile by executing:
25
+
26
+ $ bundle add elf_utils
27
+
28
+ If bundler is not being used to manage dependencies, install the gem by executing:
29
+
30
+ $ gem install elf_utils
31
+
32
+ ## Usage
33
+ ElfUtils covers a lot of ELF & DWARF functionality. The best source for usage
34
+ will be the documentation. This is a small example of using ElfUtils to
35
+ read a complex type from the memory of a running process, given an executable
36
+ with DWARF debugging information to determine types.
37
+
38
+ ```ruby
39
+ require "elf_utils"
40
+
41
+ # open an ELF file, in this case an executable
42
+ elf_file = ElfUtils.open("a.out")
43
+
44
+ # get the Symbol instance for a global variable
45
+ symbol = elf_file.symbol(:some_global_var) # => #<ElfUtils::Symbol ...>
46
+
47
+ # get the address of the symbol
48
+ addr = symbol.addr # => 0x40001000
49
+
50
+ # relocate the load segments to account for ASLR. Addresses for load segments
51
+ # extracted from /proc/pid/map.
52
+ elf_file.load_segments[0].relocate(text_addr)
53
+ elf_file.load_segments[1].relocate(data_addr)
54
+
55
+ # get the address of the symbol after relocation
56
+ addr = symbol.addr # => 0x90301000
57
+
58
+ # get the data type for the symbol; requires DWARF debug information
59
+ type = symbol.ctype # => #<CTypes::Struct ...>
60
+
61
+ # open the memory for a process running this executable, and read the value of
62
+ # the global variable.
63
+ value = File.open(File.join("/proc", pid, "mem")) do |mem|
64
+ # read the raw bytes for the variable
65
+ bytes = mem.pread(type.size, addr) # => "\xef\x99\xde... "
66
+
67
+ # unpack the bytes
68
+ type.unpack_one(bytes) # => { field: val, ... }
69
+ end
70
+ ```
71
+
72
+ ### bin/dump_ctypes.rb
73
+ [bin/dump_ctypes.rb] reads the `.debug_info` section of an ELF file, and
74
+ generates ruby code to create each data type found in the binary. This is
75
+ particularly useful for exporting types for use with foreign-function interface
76
+ (FFI).
77
+
78
+ ```sh
79
+ ~/src/elf_utils> bundle exec ./bin/dump_ctypes.rb spec/data/complex_64be-dwarf64-v5 > types.rb
80
+ cu[ 0] 0x0 ( 0kb) spec/data/test.c
81
+ cu[ 1] 0xdb ( 1kb) spec/data/types.c
82
+ ~/src/elf_utils> head types.rb
83
+ module Types
84
+ extend CTypes::Helpers
85
+
86
+ def self.with_bitfield
87
+ @with_bitfield ||= CTypes::Struct.builder()
88
+ .name("with_bitfield")
89
+ .endian(:big)
90
+ .attribute(bitfield {
91
+ field :a, offset: 31, bits: 1, signed: false
92
+ field :b, offset: 29, bits: 2, signed: true
93
+ ```
94
+
95
+ ## Roadmap
96
+
97
+ See the [open issues](https://github.com/cisco-open/ruby-elf_utils/issues) for
98
+ a list of proposed features (and known issues).
99
+
100
+ ## Development
101
+
102
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
103
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
104
+ prompt that will allow you to experiment.
105
+
106
+ To install this gem onto your local machine, run `bundle exec rake install`. To
107
+ release a new version, update the version number in `version.rb`, and then run
108
+ `bundle exec rake release`, which will create a git tag for the version, push
109
+ git commits and the created tag, and push the `.gem` file to
110
+ [rubygems.org](https://rubygems.org).
111
+
112
+ Running the test suite requires a recent version of llvm be installed. The
113
+ tests use `llvm-readelf` and `llvm-dwarfdump` for verification.
114
+
115
+ ## Contributing
116
+
117
+ Contributions are what make the open source community such an amazing place to
118
+ learn, inspire, and create. Any contributions you make are **greatly
119
+ appreciated**. For detailed contributing guidelines, please see
120
+ [CONTRIBUTING.md](CONTRIBUTING.md)
121
+
122
+ ## License
123
+
124
+ The gem is available as open source under the terms of the
125
+ [MIT License](https://opensource.org/licenses/MIT). License. See
126
+ [LICENSE.txt](LICENSE.txt) for more information.
data/Rakefile ADDED
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+ require "bundler/gem_tasks"
5
+ require "rake/clean"
6
+ require "rspec/core/rake_task"
7
+ require "standard/rake"
8
+ require "rake/extensiontask"
9
+
10
+ RSpec::Core::RakeTask.new(:spec)
11
+
12
+ Rake::ExtensionTask.new "elf_utils" do |ext|
13
+ ext.lib_dir = "lib/elf_utils"
14
+ end
15
+
16
+ task default: %i[compile]
17
+ task spec: %i[compile spec_data]
18
+
19
+ desc "Build the test data used in the specs"
20
+ task :spec_data
21
+
22
+ debug = {
23
+ "release" => "-O2",
24
+ "dwarf64-v5" => "-gdwarf-5 -gdwarf64 -O0",
25
+ "dwarf32-v5" => "-gdwarf-5 -gdwarf32 -O0",
26
+ "dwarf64-v4" => "-gdwarf-4 -gdwarf64 -O0",
27
+ "dwarf32-v4" => "-gdwarf-4 -gdwarf32 -O0",
28
+ "dwarf64-v3" => "-gdwarf-3 -gdwarf64 -O0",
29
+ "dwarf32-v3" => "-gdwarf-3 -gdwarf32 -O0",
30
+ "dwarf32-v2" => "-gdwarf-2 -gdwarf32 -O0"
31
+ }
32
+ target = {
33
+ "32le" => "--target=i386-pc-linux-gnu",
34
+ "64le" => "--target=x86_64-pc-linux-gnu",
35
+ "32be" => "--target=mips-pc-linux-gnu",
36
+ "64be" => "--target=mips64-pc-linux-gnu"
37
+ }
38
+ file_types = {
39
+ "" => "-fuse-ld=lld -nostdlib", # executable
40
+ ".o" => "-c", # object file
41
+ ".so" => "-fPIC -fuse-ld=lld --shared -nostdlib" # shared object file
42
+ }
43
+
44
+ # basic elf/dwarf files
45
+ target.each do |target, target_flag|
46
+ file_types.each do |ext, type_flag|
47
+ debug.each do |debug, debug_flag|
48
+ next if debug =~ /64/ && target =~ /32/
49
+
50
+ path = "spec/data/test_#{target}-#{debug}#{ext}"
51
+ task spec_data: path
52
+ CLEAN << path
53
+ file path => "spec/data/test.c" do |task|
54
+ sh "#{ENV["CC"] || "clang"} #{debug_flag} #{target_flag} #{type_flag} #{task.source} -o #{task.name}"
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ # DWARF files with multiple compilation units, including the types for testing
61
+ target.each do |target, target_flag|
62
+ debug.each do |debug, debug_flag|
63
+ next if debug == "release"
64
+ next if debug =~ /64/ && target =~ /32/
65
+
66
+ # grab the flags for an executable build
67
+ type_flag = file_types[""]
68
+
69
+ path = "spec/data/complex_#{target}-#{debug}"
70
+ task spec_data: path
71
+ CLEAN << path
72
+ file path => ["spec/data/test.c", "spec/data/types.c"] do |task|
73
+ sh "#{ENV["CC"] || "clang"} #{debug_flag} #{target_flag} #{type_flag} #{task.sources.join(" ")} -o #{task.name}"
74
+ end
75
+ end
76
+ end
data/SECURITY.md ADDED
@@ -0,0 +1,57 @@
1
+ # Security Policies and Procedures
2
+
3
+ This document outlines security procedures and general policies for the
4
+ `elf_utils` project.
5
+
6
+ - [Disclosing a security issue](#disclosing-a-security-issue)
7
+ - [Vulnerability management](#vulnerability-management)
8
+ - [Suggesting changes](#suggesting-changes)
9
+
10
+ ## Disclosing a security issue
11
+
12
+ The `elf_utils` maintainers take all security issues in the project
13
+ seriously. Thank you for improving the security of `elf_utils`. We
14
+ appreciate your dedication to responsible disclosure and will make every effort
15
+ to acknowledge your contributions.
16
+
17
+ `elf_utils` leverages GitHub's private vulnerability reporting.
18
+
19
+ To learn more about this feature and how to submit a vulnerability report,
20
+ review [GitHub's documentation on private reporting](https://docs.github.com/code-security/security-advisories/guidance-on-reporting-and-writing-information-about-vulnerabilities/privately-reporting-a-security-vulnerability).
21
+
22
+ Here are some helpful details to include in your report:
23
+
24
+ - a detailed description of the issue
25
+ - the steps required to reproduce the issue
26
+ - versions of the project that may be affected by the issue
27
+ - if known, any mitigations for the issue
28
+
29
+ A maintainer will acknowledge the report within three (3) business days, and
30
+ will send a more detailed response within an additional three (3) business days
31
+ indicating the next steps in handling your report.
32
+
33
+ If you've been unable to successfully draft a vulnerability report via GitHub
34
+ or have not received a response during the alloted response window, please
35
+ reach out via the [Cisco Open security contact email](mailto:oss-security@cisco.com).
36
+
37
+ After the initial reply to your report, the maintainers will endeavor to keep
38
+ you informed of the progress towards a fix and full announcement, and may ask
39
+ for additional information or guidance.
40
+
41
+ ## Vulnerability management
42
+
43
+ When the maintainers receive a disclosure report, they will assign it to a
44
+ primary handler.
45
+
46
+ This person will coordinate the fix and release process, which involves the
47
+ following steps:
48
+
49
+ - confirming the issue
50
+ - determining affected versions of the project
51
+ - auditing code to find any potential similar problems
52
+ - preparing fixes for all releases under maintenance
53
+
54
+ ## Suggesting changes
55
+
56
+ If you have suggestions on how this process could be improved please submit an
57
+ issue or pull request.
data/elf_utils.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/elf_utils/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "elf_utils"
7
+ spec.version = ElfUtils::VERSION
8
+ spec.authors = ["David M. Lary"]
9
+ spec.email = ["dmlary@gmail.com"]
10
+
11
+ spec.summary = "Library for parsing ELF files and DWARF debugging information"
12
+ # spec.description = "TODO: Write a longer description or delete this line."
13
+ spec.homepage = "https://github.com/cisco-open/elf_utils"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.1.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.extensions = %w[ext/elf_utils/extconf.rb]
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+ spec.add_dependency "ctypes", "~> 0.2"
38
+
39
+ # For more information and examples about making a new gem, check out our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ end
@@ -0,0 +1,53 @@
1
+ /*
2
+ * SPDX-FileCopyrightText: 2025 Cisco
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ #include <ruby.h>
7
+
8
+ static VALUE
9
+ uleb128_unpack_one(int argc, VALUE *argv, VALUE self)
10
+ {
11
+ if (argc < 1) {
12
+ rb_raise(rb_eArgError,
13
+ "wrong number of arguments (given %d, expected at least 1)", argc);
14
+ }
15
+
16
+ char *buf = StringValuePtr(argv[0]);
17
+ size_t len = rb_str_length(argv[0]);
18
+ size_t offset = 0;
19
+ uint64_t result = 0; // ran into conversion issues when using 128bit
20
+ int shift = 0;
21
+
22
+ while (offset < len) {
23
+ uint8_t byte = *(buf + offset++);
24
+ result |= (uint64_t)(byte & 0x7F) << shift;
25
+
26
+ // If the MSB is not set, we're done
27
+ if ((byte & 0x80) == 0) {
28
+ // not returning a bigint here because the dry-types enum does not
29
+ // return the correct value for BigInts.
30
+ return rb_ary_new3(2, rb_uint2inum(result),
31
+ rb_str_substr(argv[0], offset, len - offset));
32
+ }
33
+
34
+ // Move to the next 7 bits
35
+ shift += 7;
36
+ }
37
+
38
+ rb_raise(rb_eRuntimeError, "ULEB128 string did not contain a terminator");
39
+ }
40
+
41
+ // Initialization function called when the extension is loaded
42
+ void
43
+ Init_elf_utils()
44
+ {
45
+ // Define the class, if not defined already, and bind the C function
46
+ VALUE rb_elf_utils = rb_define_module("ElfUtils");
47
+ VALUE rb_types = rb_define_module_under(rb_elf_utils, "Types");
48
+ VALUE rb_uleb128 = rb_define_module_under(rb_types, "ULEB128");
49
+
50
+ // Define the class method (static function) in the class
51
+ rb_define_module_function(
52
+ rb_uleb128, "unpack_one", uleb128_unpack_one, -1);
53
+ }
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile "elf_utils/elf_utils"