Exemple #1
0
// Returns a buffer pointing to a member file containing a given symbol.
// This function is thread-safe.
MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
  auto ItOrErr = Sym->getMember();
  error(ItOrErr,
        Twine("Could not get the member for symbol ") + Sym->getName());
  Archive::child_iterator It = *ItOrErr;

  // Return an empty buffer if we have already returned the same buffer.
  if (Seen[It->getChildOffset()].test_and_set())
    return MemoryBufferRef();
  ErrorOr<MemoryBufferRef> Ret = It->getMemoryBufferRef();
  error(Ret, Twine("Could not get the buffer for the member defining symbol ") +
                 Sym->getName());
  return *Ret;
}
Exemple #2
0
extern "C" const char*
LLVMRustArchiveReadSection(Archive *ar, char *name, size_t *size) {
    for (Archive::child_iterator child = ar->child_begin(),
                                   end = ar->child_end();
         child != end; ++child) {
        StringRef sect_name;
        error_code err = child->getName(sect_name);
        if (err) continue;
        if (sect_name.trim(" ") == name) {
            StringRef buf = child->getBuffer();
            *size = buf.size();
            return buf.data();
        }
    }
    return NULL;
}
/// @brief Dumps each object file in \a Arc;
static void dumpArchive(const Archive *Arc) {
  for (Archive::child_iterator ArcI = Arc->child_begin(),
                               ArcE = Arc->child_end();
                               ArcI != ArcE; ++ArcI) {
    ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
    if (std::error_code EC = ChildOrErr.getError()) {
      // Ignore non-object files.
      if (EC != object_error::invalid_file_type)
        reportError(Arc->getFileName(), EC.message());
      continue;
    }

    if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
      dumpObject(Obj);
    else
      reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
  }
}
Exemple #4
0
/// @brief Dumps each object file in \a Arc;
static void dumpArchive(const Archive *Arc) {
  for (Archive::child_iterator ArcI = Arc->begin_children(),
                               ArcE = Arc->end_children();
                               ArcI != ArcE; ++ArcI) {
    OwningPtr<Binary> child;
    if (error_code EC = ArcI->getAsBinary(child)) {
      // Ignore non-object files.
      if (EC != object_error::invalid_file_type)
        reportError(Arc->getFileName(), EC.message());
      continue;
    }

    if (ObjectFile *Obj = dyn_cast<ObjectFile>(child.get()))
      dumpObject(Obj);
    else
      reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
  }
}
Exemple #5
0
static void dumpSymbolNamesFromFile(std::string &Filename) {
  ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
      MemoryBuffer::getFileOrSTDIN(Filename);
  if (error(BufferOrErr.getError(), Filename))
    return;
  std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());

  LLVMContext &Context = getGlobalContext();
  ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer, &Context);
  if (error(BinaryOrErr.getError(), Filename))
    return;
  Buffer.release();
  std::unique_ptr<Binary> Bin(BinaryOrErr.get());

  if (Archive *A = dyn_cast<Archive>(Bin.get())) {
    if (ArchiveMap) {
      Archive::symbol_iterator I = A->symbol_begin();
      Archive::symbol_iterator E = A->symbol_end();
      if (I != E) {
        outs() << "Archive map\n";
        for (; I != E; ++I) {
          ErrorOr<Archive::child_iterator> C = I->getMember();
          if (error(C.getError()))
            return;
          ErrorOr<StringRef> FileNameOrErr = C.get()->getName();
          if (error(FileNameOrErr.getError()))
            return;
          StringRef SymName = I->getName();
          outs() << SymName << " in " << FileNameOrErr.get() << "\n";
        }
        outs() << "\n";
      }
    }

    for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
         I != E; ++I) {
      ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary(&Context);
      if (ChildOrErr.getError())
        continue;
      if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
        if (!checkMachOAndArchFlags(O, Filename))
          return;
        outs() << "\n";
        if (isa<MachOObjectFile>(O)) {
          outs() << Filename << "(" << O->getFileName() << ")";
        } else
          outs() << O->getFileName();
        outs() << ":\n";
        dumpSymbolNamesFromObject(O, false);
      }
    }
    return;
  }
  if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
    // If we have a list of architecture flags specified dump only those.
    if (!ArchAll && ArchFlags.size() != 0) {
      // Look for a slice in the universal binary that matches each ArchFlag.
      bool ArchFound;
      for (unsigned i = 0; i < ArchFlags.size(); ++i) {
        ArchFound = false;
        for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
                                                   E = UB->end_objects();
             I != E; ++I) {
          if (ArchFlags[i] == I->getArchTypeName()) {
            ArchFound = true;
            ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
                I->getAsObjectFile();
            std::unique_ptr<Archive> A;
            if (ObjOrErr) {
              std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
              if (ArchFlags.size() > 1) {
                outs() << "\n" << Obj->getFileName() << " (for architecture "
                       << I->getArchTypeName() << ")"
                       << ":\n";
              }
              dumpSymbolNamesFromObject(Obj.get(), false);
            } else if (!I->getAsArchive(A)) {
              for (Archive::child_iterator AI = A->child_begin(),
                                           AE = A->child_end();
                   AI != AE; ++AI) {
                ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
                    AI->getAsBinary(&Context);
                if (ChildOrErr.getError())
                  continue;
                if (SymbolicFile *O =
                        dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
                  outs() << "\n" << A->getFileName();
                  outs() << "(" << O->getFileName() << ")";
                  if (ArchFlags.size() > 1) {
                    outs() << " (for architecture " << I->getArchTypeName()
                           << ")";
                  }
                  outs() << ":\n";
                  dumpSymbolNamesFromObject(O, false);
                }
              }
            }
          }
        }
        if (!ArchFound) {
          error(ArchFlags[i],
                "file: " + Filename + " does not contain architecture");
          return;
        }
      }
      return;
    }
    // No architecture flags were specified so if this contains a slice that
    // matches the host architecture dump only that.
    if (!ArchAll) {
      StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
      for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
                                                 E = UB->end_objects();
           I != E; ++I) {
        if (HostArchName == I->getArchTypeName()) {
          ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
          std::unique_ptr<Archive> A;
          if (ObjOrErr) {
            std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
            dumpSymbolNamesFromObject(Obj.get(), false);
          } else if (!I->getAsArchive(A)) {
            for (Archive::child_iterator AI = A->child_begin(),
                                         AE = A->child_end();
                 AI != AE; ++AI) {
              ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
                  AI->getAsBinary(&Context);
              if (ChildOrErr.getError())
                continue;
              if (SymbolicFile *O =
                      dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
                outs() << "\n" << A->getFileName() << "(" << O->getFileName()
                       << ")"
                       << ":\n";
                dumpSymbolNamesFromObject(O, false);
              }
            }
          }
          return;
        }
      }
    }
    // Either all architectures have been specified or none have been specified
    // and this does not contain the host architecture so dump all the slices.
    bool moreThanOneArch = UB->getNumberOfObjects() > 1;
    for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
                                               E = UB->end_objects();
         I != E; ++I) {
      ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
      std::unique_ptr<Archive> A;
      if (ObjOrErr) {
        std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
        if (moreThanOneArch)
          outs() << "\n";
        outs() << Obj->getFileName();
        if (isa<MachOObjectFile>(Obj.get()) && moreThanOneArch)
          outs() << " (for architecture " << I->getArchTypeName() << ")";
        outs() << ":\n";
        dumpSymbolNamesFromObject(Obj.get(), false);
      } else if (!I->getAsArchive(A)) {
        for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
             AI != AE; ++AI) {
          ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
              AI->getAsBinary(&Context);
          if (ChildOrErr.getError())
            continue;
          if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
            outs() << "\n" << A->getFileName();
            if (isa<MachOObjectFile>(O)) {
              outs() << "(" << O->getFileName() << ")";
              if (moreThanOneArch)
                outs() << " (for architecture " << I->getArchTypeName() << ")";
            } else
              outs() << ":" << O->getFileName();
            outs() << ":\n";
            dumpSymbolNamesFromObject(O, false);
          }
        }
      }
    }
    return;
  }
  if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
    if (!checkMachOAndArchFlags(O, Filename))
      return;
    dumpSymbolNamesFromObject(O, true);
    return;
  }
  error("unrecognizable file type", Filename);
  return;
}
Exemple #6
0
static void dumpSymbolNamesFromFile(std::string &Filename) {
  std::unique_ptr<MemoryBuffer> Buffer;
  if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
    return;

  LLVMContext &Context = getGlobalContext();
  ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.release(), &Context);
  if (error(BinaryOrErr.getError(), Filename))
    return;
  std::unique_ptr<Binary> Bin(BinaryOrErr.get());

  if (Archive *A = dyn_cast<Archive>(Bin.get())) {
    if (ArchiveMap) {
      Archive::symbol_iterator I = A->symbol_begin();
      Archive::symbol_iterator E = A->symbol_end();
      if (I != E) {
        outs() << "Archive map\n";
        for (; I != E; ++I) {
          Archive::child_iterator C;
          StringRef SymName;
          StringRef FileName;
          if (error(I->getMember(C)))
            return;
          if (error(I->getName(SymName)))
            return;
          if (error(C->getName(FileName)))
            return;
          outs() << SymName << " in " << FileName << "\n";
        }
        outs() << "\n";
      }
    }

    for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
         I != E; ++I) {
      std::unique_ptr<Binary> Child;
      if (I->getAsBinary(Child, &Context))
        continue;
      if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
        outs() << O->getFileName() << ":\n";
        dumpSymbolNamesFromObject(O);
      }
    }
    return;
  }
  if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
    for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
                                               E = UB->end_objects();
         I != E; ++I) {
      std::unique_ptr<ObjectFile> Obj;
      if (!I->getAsObjectFile(Obj)) {
        outs() << Obj->getFileName() << ":\n";
        dumpSymbolNamesFromObject(Obj.get());
      }
    }
    return;
  }
  if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
    dumpSymbolNamesFromObject(O);
    return;
  }
  error("unrecognizable file type", Filename);
  return;
}