Module: OS::Linux::Elf Private

Defined in:
os/linux/elf.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper functions for working with ELF objects.

Class Method Summary collapse

Class Method Details

.expand_elf_dst(str, ref, repl) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'os/linux/elf.rb', line 233

def self.expand_elf_dst(str, ref, repl)
  # ELF gABI rules for DSTs:
  #   - Longest possible sequence using the rules (greedy).
  #   - Must start with a $ (enforced by caller).
  #   - Must follow $ with one underscore or ASCII [A-Za-z] (caller
  #     follows these rules for REF) or '{' (start curly quoted name).
  #   - Must follow first two characters with zero or more [A-Za-z0-9_]
  #     (enforced by caller) or '}' (end curly quoted name).
  # (from https://github.com/bminor/glibc/blob/41903cb6f460d62ba6dd2f4883116e2a624ee6f8/elf/dl-load.c#L182-L228)

  # In addition to capturing a token, also attempt to capture opening/closing braces and check that they are not
  # mismatched before expanding.
  str.gsub(/\$({?)([a-zA-Z_][a-zA-Z0-9_]*)(}?)/) do |orig_str|
    has_opening_brace = ::Regexp.last_match(1).present?
    matched_text = ::Regexp.last_match(2)
    has_closing_brace = ::Regexp.last_match(3).present?
    if (matched_text == ref) && (has_opening_brace == has_closing_brace)
      repl
    else
      orig_str
    end
  end
end