def filter_inclusive(self, registers): """ Filter the given registers such that registers matching the filter's criteria are included, while all others are excluded (removed) """ logger.info("Applying filter: including {d}".format(d=str(self))) result = list(itertools.filterfalse(self._do_filter, registers)) logger.debug("{count} registers remaining after {name}".format( name=str(type(self).__name__), count=str(len(result)))) return result
def generate(self, objects, outpath): try: outfile_path = os.path.abspath(os.path.join(outpath, "shoulder.h")) logger.info("Generating C Header: " + str(outfile_path)) with open(outfile_path, "w") as outfile: self._generate(outfile, objects) except Exception as e: msg = "{g} failed to generate output {out}: {exception}".format( g=str(type(self).__name__), out=outpath, exception=e) raise ShoulderGeneratorException(msg)
def filter_exclusive(self, registers): """ Filter the given registers such that registers matching the filter's criteria are excluded (removed) """ logger.info("Applying filter: excluding {d}".format(d=str(self))) result = list(filter(self._do_filter, registers)) logger.debug("{name} removed {count} registers".format( name=str(type(self).__name__), count=str(len(registers) - len(result)))) return result
def parse_registers(spec_path): if not os.path.exists(spec_path): msg = "Failed to parse registers, spec not found at: " + str(spec_path) logger.error(msg) raise ShoulderParserException(msg) logger.info("Parsing registers from: " + str(spec_path)) paths = glob.glob(spec_path + "/*.xml") regs = [] parser = ArmV8XmlParser() for path in paths: results = parser.parse_registers(path) if results: regs.append(results[0]) return regs
def generate(self, regs, outpath): try: outfile_path = os.path.abspath(os.path.join(outpath, "shoulder.h")) logger.info("Generating C Header: " + str(outfile_path)) regs = transforms["remove_reserved_0"].transform(regs) regs = transforms["remove_reserved_1"].transform(regs) regs = transforms["remove_reserved_sign_extended"].transform(regs) regs = transforms["remove_implementation_defined"].transform(regs) regs = transforms["special_to_underscore"].transform(regs) regs = transforms["remove_redundant_am"].transform(regs) regs = transforms["remove_redundant_fields"].transform(regs) regs = transforms["unique_fieldset_names"].transform(regs) regs = filters["no_access_mechanism"].filter_exclusive(regs) if config.encoded_functions: msg = "Encoded accessors are only supported for aarch64 " msg += "registers (aarch32 and external not supported)" logger.warn(msg) regs = filters["aarch64"].filter_inclusive(regs) self.gadgets["shoulder.header_depends"].includes = [ "<stdint.h>", "aarch32_gcc_accessor_macros.h", "aarch64_gcc_accessor_macros.h" ] unique = [] for reg in regs: external_mechs = reg.access_mechanisms["ldr"] + \ reg.access_mechanisms["str"] for mech in external_mechs: if mech.component not in unique: unique.append(mech.component) self.gadgets["shoulder.external_component"].components = unique with open(outfile_path, "w") as outfile: self._generate(outfile, regs) except Exception as e: msg = "{g} failed to generate output {out}: {exception}".format( g=str(type(self).__name__), out=outpath, exception=e) raise ShoulderGeneratorException(msg)
def generate_all(objects, outdir): logger.info("Generating outputs to: " + str(outdir)) all_generators = [cls for cls in abstract_generator.AbstractGenerator.__subclasses__()] for generator_class in all_generators: sub_outdir = os.path.abspath(os.path.join(outdir, generator_class.__name__)) if not os.path.exists(sub_outdir): os.makedirs(sub_outdir) if os.path.exists(config.accessor_macros_path): shutil.copy(config.accessor_macros_path, sub_outdir) if os.path.exists(config.encoded_macros_path): shutil.copy(config.encoded_macros_path, sub_outdir) g = generator_class() g.generate(objects, sub_outdir) logger.info("Generation complete")
def generate_all(regs, outdir): logger.info("Generating outputs to: " + str(outdir)) all_generators = [ cls for cls in abstract_generator.AbstractGenerator.__subclasses__() ] for generator_class in all_generators: sub_outdir = os.path.abspath( os.path.join(outdir, generator_class.__name__)) if os.path.exists(sub_outdir): shutil.rmtree(sub_outdir) if os.path.exists(config.shoulder_include_dir): shutil.copytree(config.shoulder_include_dir, sub_outdir) g = generator_class() g.generate(copy.deepcopy(regs), sub_outdir) logger.info("Generation complete")
def transform(self, registers): """ Transform the given list of registers """ logger.info("Applying transform: {d}".format(d=str(self))) return list(map(self.do_transform, registers))