def test_NamegenGetsWiped(self):
        """Test that driver-generated temp files can get wiped
    (no path canonicalization problems, etc.).
    This assumes that the default option is to not "-save-temps".
    """
        temp_out = pathtools.normalize(tempfile.NamedTemporaryFile().name)
        temp_in1 = pathtools.normalize(tempfile.NamedTemporaryFile().name)
        temp_in2 = pathtools.normalize(tempfile.NamedTemporaryFile().name)
        namegen = driver_tools.TempNameGen([temp_in1, temp_in2], temp_out)
        t_gen_out = namegen.TempNameForOutput('pexe')
        t_gen_in = namegen.TempNameForInput(temp_in1, 'bc')

        # Touch/create them (append to not truncate).
        fp = driver_log.DriverOpen(t_gen_out, 'a')
        self.assertTrue(os.path.exists(t_gen_out))
        self.tempfiles.append(fp)
        fp.close()

        fp2 = driver_log.DriverOpen(t_gen_in, 'a')
        self.assertTrue(os.path.exists(t_gen_in))
        self.tempfiles.append(fp2)
        fp2.close()

        # Now wipe!
        driver_log.TempFiles.wipe()
        self.assertFalse(os.path.exists(t_gen_out))
        self.assertFalse(os.path.exists(t_gen_in))
Example #2
0
    def nameGenTemps(self):
        temp_out = pathtools.normalize(tempfile.NamedTemporaryFile().name)
        temp_in1 = pathtools.normalize(tempfile.NamedTemporaryFile().name)
        temp_in2 = pathtools.normalize(tempfile.NamedTemporaryFile().name)
        namegen = driver_tools.TempNameGen([temp_in1, temp_in2], temp_out)
        t_gen_out = namegen.TempNameForOutput('pexe')
        t_gen_in = namegen.TempNameForInput(temp_in1, 'bc')

        # Touch/create them (append to not truncate).
        fp = driver_log.DriverOpen(t_gen_out, 'a')
        self.assertTrue(os.path.exists(t_gen_out))
        self.tempfiles.append(fp)
        fp.close()

        fp2 = driver_log.DriverOpen(t_gen_in, 'a')
        self.assertTrue(os.path.exists(t_gen_in))
        self.tempfiles.append(fp2)
        fp2.close()
        return t_gen_out, t_gen_in
Example #3
0
def GetThinArchiveData(archive_filename, member, strtab_data):
    # Get member's filename (relative to the archive) and open the member
    # ourselves to check the data.
    member_filename = GetMemberFilename(member, strtab_data)
    member_filename = pathtools.join(
        pathtools.dirname(pathtools.abspath(archive_filename)),
        member_filename)
    member_fp = driver_log.DriverOpen(member_filename, 'rb')
    data = member_fp.read(member.size)
    member_fp.close()
    return data
Example #4
0
def GetArchiveType(filename):
    fp = driver_log.DriverOpen(filename, "rb")

    # Read the archive magic header
    magic = fp.read(len(AR_MAGIC))
    assert (magic in [AR_MAGIC, THIN_MAGIC])

    # Find a regular file or symbol table
    empty_file = True
    found_type = ''
    strtab_data = ''
    while not found_type:
        member = MemberHeader(fp)
        if member.error == 'EOF':
            break
        elif member.error:
            driver_log.Log.Fatal("%s: %s", filename, member.error)

        empty_file = False

        if member.is_regular_file:
            if not magic == THIN_MAGIC:
                data = fp.read(member.size)
            else:
                # For thin archives, do not read the data section.
                # We instead must get at the member indirectly.
                data = GetThinArchiveData(filename, member, strtab_data)

            if data.startswith('BC'):
                found_type = 'archive-bc'
            else:
                elf_header = elftools.DecodeELFHeader(data, filename)
                if elf_header:
                    found_type = 'archive-%s' % elf_header.arch
        elif member.is_strtab:
            # We need the strtab data to get long filenames.
            data = fp.read(member.size)
            strtab_data = data
            continue
        else:
            # Other symbol tables we can just skip ahead.
            data = fp.read(member.size)
            continue

    if empty_file:
        # Empty archives are treated as bitcode ones.
        found_type = 'archive-bc'
    elif not found_type:
        driver_log.Log.Fatal("%s: Unable to determine archive type", filename)

    fp.close()
    return found_type
Example #5
0
def GetBitcodeMagic(filename):
    fp = driver_log.DriverOpen(filename, 'rb')
    header = fp.read(4)
    driver_log.DriverClose(fp)
    return header
Example #6
0
def ParseLinkerScript(filename):
    fp = driver_log.DriverOpen(filename, 'r')

    ret = []
    stack = []
    expect = ''  # Expected next token
    while True:
        token = GetNextToken(fp)
        if token is None:
            # Tokenization error
            return None

        if not token:
            # EOF
            break

        if expect:
            if token == expect:
                expect = ''
                continue
            else:
                return None

        if not stack:
            if token == 'INPUT':
                expect = '('
                stack.append(token)
            elif token == 'GROUP':
                expect = '('
                ret.append('--start-group')
                stack.append(token)
            elif token == 'OUTPUT_FORMAT':
                expect = '('
                stack.append(token)
            elif token == 'EXTERN':
                expect = '('
                stack.append(token)
            elif token == ';':
                pass
            else:
                return None
        else:
            if token == ')':
                section = stack.pop()
                if section == 'AS_NEEDED':
                    ret.append('--no-as-needed')
                elif section == 'GROUP':
                    ret.append('--end-group')
            elif token == 'AS_NEEDED':
                expect = '('
                ret.append('--as-needed')
                stack.append('AS_NEEDED')
            elif stack[-1] == 'OUTPUT_FORMAT':
                # Ignore stuff inside OUTPUT_FORMAT
                pass
            elif stack[-1] == 'EXTERN':
                ret.append('--undefined=' + token)
            else:
                ret.append('-l:' + token)

    fp.close()
    return ret
Example #7
0
def IsArchive(filename):
    fp = driver_log.DriverOpen(filename, "rb")
    magic = fp.read(len(AR_MAGIC))
    fp.close()
    return magic in [AR_MAGIC, THIN_MAGIC]