File: architecture.py

package info (click to toggle)
android-platform-development 10.0.0%2Br36-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 135,564 kB
  • sloc: java: 160,253; xml: 127,434; python: 40,579; cpp: 17,579; sh: 2,569; javascript: 1,612; ansic: 879; lisp: 261; ruby: 183; makefile: 172; sql: 140; perl: 88
file content (58 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Abstraction layer for different ABIs."""

import re
import symbol

def UnpackLittleEndian(word):
  """Split a hexadecimal string in little endian order."""
  return [word[x:x+2] for x in range(len(word) - 2, -2, -2)]


ASSEMBLE = 'as'
DISASSEMBLE = 'objdump'
LINK = 'ld'
UNPACK = 'unpack'

OPTIONS = {
    'x86': {
        ASSEMBLE: ['--32'],
        LINK: ['-melf_i386']
    }
}


class Architecture(object):
  """Creates an architecture abstraction for a given ABI.

  Args:
    name: The abi name, as represented in a tombstone.
  """

  def __init__(self, name):
    symbol.ARCH = name
    self.toolchain = symbol.FindToolchain()
    self.options = OPTIONS.get(name, {})

  def Assemble(self, args):
    """Generates an assembler command, appending the given args."""
    return [symbol.ToolPath(ASSEMBLE)] + self.options.get(ASSEMBLE, []) + args

  def Link(self, args):
    """Generates a link command, appending the given args."""
    return [symbol.ToolPath(LINK)] + self.options.get(LINK, []) + args

  def Disassemble(self, args):
    """Generates a disassemble command, appending the given args."""
    return ([symbol.ToolPath(DISASSEMBLE)] + self.options.get(DISASSEMBLE, []) +
            args)

  def WordToBytes(self, word):
    """Unpacks a hexadecimal string in the architecture's byte order.

    Args:
      word: A string representing a hexadecimal value.

    Returns:
      An array of hexadecimal byte values.
    """
    return self.options.get(UNPACK, UnpackLittleEndian)(word)