Skip to content

Instantly share code, notes, and snippets.

@michaeleisel
Created October 17, 2018 22:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeleisel/a36e74864495bae8aab3a0b93fe91ef4 to your computer and use it in GitHub Desktop.
Save michaeleisel/a36e74864495bae8aab3a0b93fe91ef4 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
raise "Usage: ./args.rb <path to executable>" unless ARGV.size == 1
new_seg_name = "__MY_TEXT"
binary = ARGV.first
sect_regex = /\s*Section (\S+)/
sects = `xcrun size -x -l -m #{binary}`.split("\n").drop_while do |line|
!line.start_with?("Segment __TEXT:")
end.drop(1).take_while do |line|
line =~ sect_regex
end.map do |line|
line.match(/^\s*Section (\S+):/)[1]
end
cmds = sects.map do |sect|
"-Wl,-rename_section,__TEXT,#{sect},#{new_seg_name},#{sect}"
end
puts "Sections:\n#{sects}\n\nLink commands:\n#{cmds.join("\n")}\n\nProtection link command:\n-Wl,-segprot,#{new_seg_name},rx,rx\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment