Giter Site home page Giter Site logo

worf's Introduction

WORF, the DWARF parser

WORF is a DWARF parser that is written in Ruby. You can use this library to parse DWARF files. I usually use this with Mach-O files or ELF files, but as long as you have an IO object that contains DWARF data, WORF will parse it.

With DWARF data, you can write some debugging utilities, but as an example I'll write a very simple version of pahole, a utility that finds holes in structs.

Example pahole

This example only works on macOS. We're going to find structs in Ruby that have holes in them (or wasted space).

First we'll use OdinFlex to find Ruby's archive file:

archive = nil

File.open(RbConfig.ruby) do |f|
  my_macho = OdinFlex::MachO.new f
  my_macho.each do |section|
    if section.symtab?
      archive = section.nlist.find_all(&:archive?).map(&:archive).uniq.first
      break
    end
  end
end

Now that we have the archive file, we're going to use OdinFlex again to process the AR file which will give us access to all of the Mach-O files stored inside. Those Mach-O files also have debugging sections that contain DWARF data, and we'll use WORF to parse that data:

File.open(archive) do |f|
  ar = OdinFlex::AR.new f
  ar.each do |object_file|
    next unless object_file.identifier =~ /\.o$/
    p object_file.identifier

    mach_o = OdinFlex::MachO.new(f)
    debug_abbrev = debug_strs = debug_info = nil

    mach_o.each do |part|
      if part.section?
        case part.sectname
        when "__debug_abbrev"
          debug_abbrev = WORF::DebugAbbrev.new f, part, mach_o.start_pos
        when "__debug_str"
          debug_strs = WORF::DebugStrings.new f, part, mach_o.start_pos
        when "__debug_info"
          debug_info = WORF::DebugInfo.new f, part, mach_o.start_pos
        end
      end
    end

    if debug_abbrev && debug_strs && debug_info
      puts "great"
      process_debug_info(debug_abbrev, debug_strs, debug_info)
      ## Now process the DWARF info
    end
    exit
  end
end

Now we can process the DWARF information and find holes in structs!

Ok, I am feeling lazy and don't want to write the rest of this program. Check in the examples folder for a full listing.

worf's People

Contributors

tenderlove avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

jhawthorn

worf's Issues

Add support to DWARF 5

I'm trying to run tenderjit in a new environment with GCC 11.1, which defaults to DWARF 5 but it seems it is not compatible with WORF.

I'm not that familiar with DWARF format even though I'm willing to give it a try :)

$ uname -a
Linux Workstation 5.13.19-2-MANJARO #1 SMP PREEMPT Sun Sep 19 21:31:53 UTC 2021 x86_64 GNU/Linux

$ gcc --version
gcc (GCC) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
$ bundle exec rake test
/home/www/.asdf/installs/ruby/3.0.2/bin/ruby -I lib misc/build-ruby-internals.rb a91ae
/home/www/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/worf-59571984c7e9/lib/worf.rb:477:in `compile_units': Only DWARF4 rn 5 (NotImplementedError)
	from /home/www/Documents/Opensource/tenderjit/misc/ruby_internals.rb:325:in `block in each_compile_unit'
	from /home/www/Documents/Opensource/tenderjit/misc/ruby_internals.rb:318:in `open'
	from /home/www/Documents/Opensource/tenderjit/misc/ruby_internals.rb:318:in `each_compile_unit'
	from /home/www/Documents/Opensource/tenderjit/misc/ruby_internals.rb:114:in `process'
	from /home/www/Documents/Opensource/tenderjit/misc/ruby_internals.rb:381:in `get_internals'
	from misc/build-ruby-internals.rb:435:in `<main>'
rake aborted!
Command failed with status (1): [/home/www/.asdf/installs/ruby/3.0.2/bin/ru...]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.