Module: Ronin::Payloads::Mixins::CCompiler
- Included in:
- CPayload
- Defined in:
- lib/ronin/payloads/mixins/c_compiler.rb
Overview
Mixin for using the C compiler.
Features
- Supports
gcc
andclang
. - Supports automatically switching to a cross compiler to cross compile for different architectures and OSes.
- Supports using
mingw32
to cross-compile for Windows.
Class Method Summary collapse
-
.cc ⇒ String?
private
The default C compiler.
-
.included(payload_class) ⇒ Object
private
Adds the
cc
,c_compiler
,arch
,vendor
, andos
params to the payload class that included CCompiler.
Instance Method Summary collapse
-
#cc ⇒ String
private
The C compiler command to use.
-
#compile_c(*source_files, output:, defs: nil, libs: nil) ⇒ Object
(also: #compile)
Compiles one or more source files using
cc
. -
#target_arch ⇒ String
private
The target architecture to compile for.
-
#target_os ⇒ String
private
The target OS to compile for.
-
#target_platform ⇒ String?
private
The target platform to compile for.
-
#target_vendor ⇒ String
private
The target vendor to compile for.
Class Method Details
.cc ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The default C compiler.
45 46 47 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 45 def self.cc ENV['CC'] end |
.included(payload_class) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds the cc
, c_compiler
, arch
, vendor
, and os
params
to the payload class that included Ronin::Payloads::Mixins::CCompiler.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 58 def self.included(payload_class) payload_class.param :cc, default: -> { cc }, desc: 'The C compiler command to use' payload_class.param :c_compiler, Core::Params::Types::Enum[ :gcc, :clang ], default: :gcc, desc: 'The C compiler to use' payload_class.param :arch, Core::Params::Types::Enum[ :"x86-64", :i686, :aarch64, :arm, :arm64, :armbe, :armbe64, :mips, :mips64, :ppc, :ppc64 ], desc: 'The target architecture' payload_class.param :vendor, Core::Params::Types::Enum[ :pc, :unknown ], desc: 'The target vendor' payload_class.param :os, Core::Params::Types::Enum[ :linux, :macos, :freebsd, :windows, :"windows-gnu", :"windows-msvc" ], desc: 'The target OS' end |
Instance Method Details
#cc ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The C compiler command to use.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 172 def cc params[:cc] || case params[:c_compiler] when :gcc if (target = target_platform) "#{target}-gcc" else 'gcc' end when :clang then 'clang' else 'cc' end end |
#compile_c(*source_files, output:, defs: nil, libs: nil) ⇒ Object Also known as: compile
Compiles one or more source files using cc
.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 206 def compile_c(*source_files, output: , defs: nil, libs: nil) target = target_platform args = [cc] if target && params[:c_compiler] == :clang args << '-target' << target end if defs case defs when Array defs.each do |value| args << "-D#{value}" end when Hash defs.each do |name,value| args << "-D#{name}=#{value}" end else raise(ArgumentError,"defs must be either an Array or a Hash: #{defs.inspect}") end end args << '-o' << output args.concat(source_files) if libs libs.each do |lib| args << "-l#{lib}" end end case system(*args) when false raise(BuildFailed,"cc command failed: #{args.join(' ')}") when nil raise(BuildFailed,"cc command not installed") end end |
#target_arch ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The target architecture to compile for.
105 106 107 108 109 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 105 def target_arch if params[:arch] params[:arch].to_s.tr('-','_') end end |
#target_os ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The target OS to compile for.
135 136 137 138 139 140 141 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 135 def target_os case params[:os] when :linux then 'linux-gnu' when :windows then 'mingw32' else params[:os].to_s end end |
#target_platform ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The target platform to compile for.
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 151 def target_platform arch = target_arch os = target_os if arch && os if (vendor = target_vendor) "#{arch}-#{vendor}-#{os}" else "#{arch}-#{os}" end end end |
#target_vendor ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The target vendor to compile for.
119 120 121 122 123 124 125 |
# File 'lib/ronin/payloads/mixins/c_compiler.rb', line 119 def target_vendor if params[:os] == :windows 'w64' elsif params[:vendor] params[:vendor].to_s end end |