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 and clang.
  • Supports automatically switching to a cross compiler to cross compile for different architectures and OSes.
  • Supports using mingw32 to cross-compile for Windows.

Since:

  • 0.2.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ccString?

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.

Returns:

  • (String, nil)

Since:

  • 0.2.0



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.

Parameters:

Since:

  • 0.2.0



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

#ccString

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.

Returns:

  • (String)

    The command name.

Since:

  • 0.2.0



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.

Parameters:

  • source_files (Array<String>)

    The source file(s) to compile.

  • output (String)

    The output file path.

  • defs (Array<String>, Hash{Symbol,String => String}, nil) (defaults to: nil)

    Additional macro definitions to pass to the compiler.

  • libs (Array<String>) (defaults to: nil)

    Libraries to link to.

Raises:

  • (ArgumentError)

    defs was not an Array or a Hash.

  • (BuildFailed)

    The cc command failed or is not installed.

Since:

  • 0.2.0



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_archString

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.

Returns:

  • (String)

    The target architecture string.

Since:

  • 0.2.0



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_osString

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.

Returns:

  • (String)

    The target OS string.

Since:

  • 0.2.0



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_platformString?

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.

Returns:

  • (String, nil)

    The target triple string, if the arch and os params are set.

Since:

  • 0.2.0



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_vendorString

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.

Returns:

  • (String)

    The target vendor string.

Since:

  • 0.2.0



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