Coverage for /Syzygy/pe/pe_relinker_util.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
71.2%1852600.C++source

Line-by-line coverage:

   1    :  // Copyright 2013 Google Inc. All Rights Reserved.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  
  15    :  #include "syzygy/pe/pe_relinker_util.h"
  16    :  
  17    :  #include "base/files/file_util.h"
  18    :  #include "syzygy/block_graph/transform.h"
  19    :  #include "syzygy/core/file_util.h"
  20    :  #include "syzygy/core/zstream.h"
  21    :  #include "syzygy/pdb/pdb_byte_stream.h"
  22    :  #include "syzygy/pdb/pdb_util.h"
  23    :  #include "syzygy/pe/find.h"
  24    :  #include "syzygy/pe/metadata.h"
  25    :  #include "syzygy/pe/pe_image_layout_builder.h"
  26    :  #include "syzygy/pe/pe_utils.h"
  27    :  #include "syzygy/pe/serialization.h"
  28    :  #include "syzygy/pe/orderers/pe_orderer.h"
  29    :  #include "syzygy/pe/transforms/add_metadata_transform.h"
  30    :  #include "syzygy/pe/transforms/add_pdb_info_transform.h"
  31    :  #include "syzygy/pe/transforms/pe_prepare_headers_transform.h"
  32    :  #include "syzygy/pe/transforms/pe_remove_empty_sections_transform.h"
  33    :  
  34    :  namespace pe {
  35    :  
  36    :  namespace {
  37    :  
  38    :  using block_graph::BlockGraph;
  39    :  using block_graph::BlockGraphTransformInterface;
  40    :  using block_graph::OrderedBlockGraph;
  41    :  using core::RelativeAddress;
  42    :  using pdb::NameStreamMap;
  43    :  using pdb::PdbByteStream;
  44    :  using pdb::PdbFile;
  45    :  using pdb::PdbInfoHeader70;
  46    :  using pdb::PdbStream;
  47    :  using pdb::WritablePdbStream;
  48    :  using pe::PETransformPolicy;
  49    :  
  50    :  // A utility class for wrapping a serialization OutStream around a
  51    :  // WritablePdbStream.
  52    :  // TODO(chrisha): We really need to centralize stream/buffer semantics in
  53    :  //     a small set of clean interfaces, and make all input/output/parsing work
  54    :  //     on these interfaces.
  55    :  class PdbOutStream : public core::OutStream {
  56    :   public:
  57  E :    explicit PdbOutStream(WritablePdbStream* pdb_stream)
  58    :        : pdb_stream_(pdb_stream) {
  59  E :      DCHECK(pdb_stream != NULL);
  60  E :    }
  61    :  
  62  E :    virtual ~PdbOutStream() { }
  63    :  
  64  E :    virtual bool Write(size_t length, const core::Byte* bytes) override {
  65  E :      return pdb_stream_->Write(length, bytes);
  66  E :    }
  67    :  
  68    :   private:
  69    :    scoped_refptr<WritablePdbStream> pdb_stream_;
  70    :  };
  71    :  
  72    :  void BuildOmapVectors(const RelativeAddressRange& input_range,
  73    :                        const ImageLayout& output_image_layout,
  74    :                        std::vector<OMAP>* omap_to,
  75  E :                        std::vector<OMAP>* omap_from) {
  76  E :    DCHECK(omap_to != NULL);
  77  E :    DCHECK(omap_from != NULL);
  78    :  
  79  E :    LOG(INFO) << "Building OMAP vectors.";
  80    :  
  81    :    // Get the range of the output image, sans headers. This is required for
  82    :    // generating OMAP information.
  83  E :    RelativeAddressRange output_range;
  84  E :    GetOmapRange(output_image_layout.sections, &output_range);
  85    :  
  86  E :    ImageSourceMap reverse_map;
  87  E :    BuildImageSourceMap(output_image_layout, &reverse_map);
  88    :  
  89  E :    ImageSourceMap forward_map;
  90  E :    if (reverse_map.ComputeInverse(&forward_map) != 0) {
  91  E :      LOG(WARNING) << "OMAPFROM not unique (there exist repeated source ranges).";
  92    :    }
  93    :  
  94    :    // Build the two OMAP vectors.
  95  E :    BuildOmapVectorFromImageSourceMap(output_range, reverse_map, omap_to);
  96  E :    BuildOmapVectorFromImageSourceMap(input_range, forward_map, omap_from);
  97  E :  }
  98    :  
  99    :  // Get a specific named stream if it already exists, otherwise create one.
 100    :  // @param stream_name The name of the stream.
 101    :  // @param name_stream_map The map containing the names of the streams in the
 102    :  //     PDB. If the stream doesn't already exist the map will be augmented with
 103    :  //     another entry.
 104    :  // @param pdb_file The PDB file to which the stream will be added.
 105    :  // @param replace_stream If true, will cause a new stream to be created even if
 106    :  //     another one already existed.
 107    :  // @return a pointer to the PDB stream on success, NULL on failure.
 108    :  PdbStream* GetOrCreatePdbStreamByName(const char* stream_name,
 109    :                                        bool replace_stream,
 110    :                                        NameStreamMap* name_stream_map,
 111  E :                                        PdbFile* pdb_file) {
 112  E :    DCHECK(name_stream_map != NULL);
 113  E :    DCHECK(pdb_file != NULL);
 114  E :    scoped_refptr<PdbStream> stream;
 115    :  
 116  E :    NameStreamMap::const_iterator name_it = name_stream_map->find(stream_name);
 117  E :    if (name_it != name_stream_map->end()) {
 118    :      // Replace the existing stream by a brand-new one if it's required.
 119  i :      if (replace_stream) {
 120  i :        stream = new PdbByteStream();
 121  i :        pdb_file->ReplaceStream(name_it->second, stream.get());
 122  i :      } else {
 123  i :        if (!pdb::EnsureStreamWritable(name_it->second, pdb_file)) {
 124  i :          LOG(ERROR) << "Failed to make " << stream_name << " stream writable.";
 125  i :          return NULL;
 126    :        }
 127  i :        stream = pdb_file->GetStream(name_it->second);
 128    :      }
 129  i :    } else {
 130  E :      stream = new PdbByteStream();
 131  E :      uint32 index = pdb_file->AppendStream(stream.get());
 132  E :      (*name_stream_map)[stream_name] = index;
 133    :    }
 134    :  
 135  E :    return stream.get();
 136  E :  }
 137    :  
 138    :  // This updates or creates the Syzygy history stream, appending the metadata
 139    :  // describing this module and transform. The history stream consists of
 140    :  // a named PDB stream with the name /Syzygy/History. It consists of:
 141    :  //
 142    :  //   uint32 version
 143    :  //   uint32 history_length
 144    :  //   serialized pe::Metadata 0
 145    :  //   ...
 146    :  //   serialized pe::Metadata history_length - 1
 147    :  //
 148    :  // If the format is changed, be sure to update this documentation and
 149    :  // pdb::kSyzygyHistoryStreamVersion (in pdb_constants.h).
 150    :  bool WriteSyzygyHistoryStream(const base::FilePath& input_path,
 151    :                                NameStreamMap* name_stream_map,
 152  E :                                PdbFile* pdb_file) {
 153    :    // Get the history stream.
 154    :    scoped_refptr<PdbStream> history_reader =
 155    :        GetOrCreatePdbStreamByName(pdb::kSyzygyHistoryStreamName,
 156    :                                   false,
 157    :                                   name_stream_map,
 158  E :                                   pdb_file);
 159    :  
 160  E :    if (history_reader == NULL) {
 161  i :      LOG(ERROR) << "Failed to get the history stream.";
 162  i :      return false;
 163    :    }
 164    :  
 165    :    scoped_refptr<WritablePdbStream> history_writer =
 166  E :        history_reader->GetWritableStream();
 167  E :    DCHECK(history_writer.get() != NULL);
 168    :  
 169    :    // Get the metadata.
 170  E :    Metadata metadata;
 171  E :    PEFile pe_file;
 172  E :    if (!pe_file.Init(input_path)) {
 173  i :      LOG(ERROR) << "Failed to initialize PE file for \"" << input_path.value()
 174    :                 << "\".";
 175  i :      return false;
 176    :    }
 177    :  
 178  E :    PEFile::Signature pe_sig;
 179  E :    pe_file.GetSignature(&pe_sig);
 180  E :    if (!metadata.Init(pe_sig)) {
 181  i :      LOG(ERROR) << "Failed to initialize metadata for \"" << input_path.value()
 182    :                 << "\".";
 183  i :      return false;
 184    :    }
 185    :  
 186    :    // Validate the history stream if it is non-empty.
 187  E :    if (history_reader->length() > 0) {
 188    :      // Read the header.
 189  i :      uint32 version = 0;
 190  i :      uint32 history_length = 0;
 191    :      if (!history_reader->Seek(0) ||
 192    :          !history_reader->Read(&version, 1) ||
 193  i :          !history_reader->Read(&history_length, 1)) {
 194  i :        LOG(ERROR) << "Failed to read existing Syzygy history stream header.";
 195  i :        return false;
 196    :      }
 197    :  
 198    :      // Check the version.
 199  i :      if (version != pdb::kSyzygyHistoryStreamVersion) {
 200  i :        LOG(ERROR) << "PDB contains unsupported Syzygy history stream version "
 201    :                   << "(got " << version << ", expected "
 202    :                   << pdb::kSyzygyHistoryStreamVersion << ").";
 203  i :        return false;
 204    :      }
 205    :  
 206    :      // Increment the history length and rewrite it.
 207  i :      history_length++;
 208  i :      history_writer->set_pos(sizeof(pdb::kSyzygyHistoryStreamVersion));
 209  i :      if (!history_writer->Write(history_length)) {
 210  i :        LOG(ERROR) << "Failed to write new Syzygy history stream length.";
 211  i :        return false;
 212    :      }
 213  i :    } else {
 214    :      // If there wasn't already a history stream, create one and write the
 215    :      // header.
 216  E :      DCHECK_EQ(0u, history_writer->pos());
 217  E :      const uint32 kHistoryLength = 1;
 218    :      if (!history_writer->Write(pdb::kSyzygyHistoryStreamVersion) ||
 219  E :          !history_writer->Write(kHistoryLength)) {
 220  i :        LOG(ERROR) << "Failed to write Syzygy history stream header.";
 221  i :        return false;
 222    :      }
 223    :    }
 224    :  
 225    :    // Append the metadata to the history.
 226  E :    history_writer->set_pos(history_writer->length());
 227  E :    PdbOutStream out_stream(history_writer.get());
 228  E :    core::OutArchive out_archive(&out_stream);
 229  E :    if (!out_archive.Save(metadata)) {
 230  i :      LOG(ERROR) << "Failed to write metadata to Syzygy history stream.";
 231  i :      return false;
 232    :    }
 233    :  
 234  E :    return true;
 235  E :  }
 236    :  
 237    :  // This writes the serialized block-graph and the image layout in a PDB stream
 238    :  // named /Syzygy/BlockGraph. If the format is changed, be sure to update this
 239    :  // documentation and pdb::kSyzygyBlockGraphStreamVersion (in pdb_constants.h).
 240    :  // The block graph stream will not include the data from the blocks of the
 241    :  // block-graph. If the strip-strings flag is set to true the strings contained
 242    :  // in the block-graph won't be saved.
 243    :  bool WriteSyzygyBlockGraphStream(const PEFile& pe_file,
 244    :                                   const ImageLayout& image_layout,
 245    :                                   bool strip_strings,
 246    :                                   bool compress,
 247    :                                   NameStreamMap* name_stream_map,
 248  E :                                   PdbFile* pdb_file) {
 249    :    // Get the redecomposition data stream.
 250    :    scoped_refptr<PdbStream> block_graph_reader =
 251    :        GetOrCreatePdbStreamByName(pdb::kSyzygyBlockGraphStreamName,
 252    :                                   true,
 253    :                                   name_stream_map,
 254  E :                                   pdb_file);
 255    :  
 256  E :    if (block_graph_reader == NULL) {
 257  i :      LOG(ERROR) << "Failed to get the block-graph stream.";
 258  i :      return false;
 259    :    }
 260  E :    DCHECK_EQ(0u, block_graph_reader->length());
 261    :  
 262    :    scoped_refptr<WritablePdbStream> block_graph_writer =
 263  E :        block_graph_reader->GetWritableStream();
 264  E :    DCHECK(block_graph_writer.get() != NULL);
 265    :  
 266    :    // Write the version of the BlockGraph stream, and whether or not its
 267    :    // contents are compressed.
 268    :    if (!block_graph_writer->Write(pdb::kSyzygyBlockGraphStreamVersion) ||
 269  E :        !block_graph_writer->Write(static_cast<unsigned char>(compress))) {
 270  i :      LOG(ERROR) << "Failed to write Syzygy BlockGraph stream header.";
 271  i :      return false;
 272    :    }
 273    :  
 274    :    // Set up the output stream.
 275  E :    PdbOutStream pdb_out_stream(block_graph_writer.get());
 276  E :    core::OutStream* out_stream = &pdb_out_stream;
 277    :  
 278    :    // If requested, compress the output.
 279  E :    scoped_ptr<core::ZOutStream> zip_stream;
 280  E :    if (compress) {
 281  E :      zip_stream.reset(new core::ZOutStream(&pdb_out_stream));
 282  E :      out_stream = zip_stream.get();
 283  E :      if (!zip_stream->Init(core::ZOutStream::kZBestCompression)) {
 284  i :        LOG(ERROR) << "Failed to initialize zlib compressor.";
 285  i :        return false;
 286    :      }
 287    :    }
 288    :  
 289  E :    core::OutArchive out_archive(out_stream);
 290    :  
 291    :    // Set up the serialization properties.
 292  E :    block_graph::BlockGraphSerializer::Attributes attributes = 0;
 293  E :    if (strip_strings)
 294  E :      attributes |= block_graph::BlockGraphSerializer::OMIT_STRINGS;
 295    :  
 296    :    // And finally, perform the serialization.
 297    :    if (!SaveBlockGraphAndImageLayout(pe_file, attributes, image_layout,
 298  E :                                      &out_archive)) {
 299  i :      LOG(ERROR) << "SaveBlockGraphAndImageLayout failed.";
 300  i :      return false;
 301    :    }
 302    :  
 303    :    // We have to flush the stream in case it's a zstream.
 304  E :    out_stream->Flush();
 305    :  
 306  E :    return true;
 307  E :  }
 308    :  
 309    :  }  // namespace
 310    :  
 311    :  bool ValidateAndInferPaths(
 312    :      const base::FilePath& input_module,
 313    :      const base::FilePath& output_module,
 314    :      bool allow_overwrite,
 315    :      base::FilePath* input_pdb,
 316  E :      base::FilePath* output_pdb) {
 317  E :    DCHECK(!input_module.empty());
 318  E :    DCHECK(!output_module.empty());
 319  E :    DCHECK_NE(reinterpret_cast<base::FilePath*>(NULL), input_pdb);
 320  E :    DCHECK_NE(reinterpret_cast<base::FilePath*>(NULL), output_pdb);
 321    :  
 322  E :    if (!base::PathExists(input_module)) {
 323  E :      LOG(ERROR) << "Input module not found: " << input_module.value();
 324  E :      return false;
 325    :    }
 326    :  
 327  E :    if (!allow_overwrite && base::PathExists(output_module)) {
 328  E :      LOG(ERROR) << "Output module exists: " << output_module.value();
 329  E :      LOG(ERROR) << "Specify --overwrite to ignore this error.";
 330  E :      return false;
 331    :    }
 332    :  
 333    :    // If no input PDB was specified then search for it.
 334  E :    if (input_pdb->empty()) {
 335  E :      LOG(INFO) << "Input PDB not specified, searching for it.";
 336    :      if (!pe::FindPdbForModule(input_module, input_pdb) ||
 337  E :          input_pdb->empty()) {
 338  i :        LOG(ERROR) << "Unable to find PDB file for module: "
 339    :                   << input_module.value();
 340  i :        return NULL;
 341    :      }
 342    :    }
 343    :  
 344  E :    if (!base::PathExists(*input_pdb)) {
 345  E :      LOG(ERROR) << "Input PDB not found: " << input_pdb->value();
 346  E :      return false;
 347    :    }
 348    :  
 349    :    // If no output PDB path is specified, infer one.
 350  E :    if (output_pdb->empty()) {
 351    :      // If the input and output DLLs have the same basename, default to writing
 352    :      // using the same PDB basename, but alongside the new module.
 353  E :      if (input_module.BaseName() == output_module.BaseName()) {
 354  E :        *output_pdb = output_module.DirName().Append(input_pdb->BaseName());
 355  E :      } else {
 356    :        // Otherwise, default to using the output basename with a PDB extension
 357    :        // added to it.
 358  E :        *output_pdb = output_module.AddExtension(L"pdb");
 359    :      }
 360    :  
 361  E :      LOG(INFO) << "Using default output PDB path: " << output_pdb->value();
 362    :    }
 363    :  
 364  E :    if (!allow_overwrite && base::PathExists(*output_pdb)) {
 365  E :      LOG(ERROR) << "Output PDB exists: " << output_pdb->value();
 366  E :      LOG(ERROR) << "Specify --overwrite to ignore this error.";
 367  E :      return false;
 368    :    }
 369    :  
 370    :    // Perform some extra checking to make sure that writes aren't going to
 371    :    // collide. This prevents us from overwriting the input, effectively
 372    :    // preventing in-place transforms. This is not fool-proof in the face of
 373    :    // weird junctions but it will catch common errors.
 374    :  
 375    :    core::FilePathCompareResult result =
 376  E :        core::CompareFilePaths(input_module, output_module);
 377  E :    if (result == core::kEquivalentFilePaths) {
 378  i :      LOG(ERROR) << "Input and output module paths are equivalent.";
 379  i :      LOG(ERROR) << "Input module path: " << input_module.value();
 380  i :      LOG(ERROR) << "Output module path: " << output_module.value();
 381  i :      return false;
 382    :    }
 383    :  
 384  E :    result = core::CompareFilePaths(*input_pdb, *output_pdb);
 385  E :    if (result == core::kEquivalentFilePaths) {
 386  i :      LOG(ERROR) << "Input and output PDB paths are equivalent.";
 387  i :      LOG(ERROR) << "Input PDB path: " << input_pdb->value();
 388  i :      LOG(ERROR) << "Output PDB path: " << output_pdb->value();
 389  i :      return false;
 390    :    }
 391    :  
 392  E :    result = core::CompareFilePaths(output_module, *output_pdb);
 393  E :    if (result == core::kEquivalentFilePaths) {
 394  i :      LOG(ERROR) << "Output module and PDB paths are equivalent.";
 395  i :      LOG(ERROR) << "Output module path: " << output_module.value();
 396  i :      LOG(ERROR) << "Output PDB path: " << output_pdb->value();
 397  i :      return false;
 398    :    }
 399    :  
 400  E :    return true;
 401  E :  }
 402    :  
 403    :  bool FinalizeBlockGraph(const base::FilePath& input_module,
 404    :                          const base::FilePath& output_pdb,
 405    :                          const GUID& pdb_guid,
 406    :                          bool add_metadata,
 407    :                          const PETransformPolicy* policy,
 408    :                          BlockGraph* block_graph,
 409  E :                          BlockGraph::Block* dos_header_block) {
 410  E :    DCHECK_NE(reinterpret_cast<PETransformPolicy*>(NULL), policy);
 411  E :    DCHECK_NE(reinterpret_cast<BlockGraph*>(NULL), block_graph);
 412  E :    DCHECK_NE(reinterpret_cast<BlockGraph::Block*>(NULL), dos_header_block);
 413  E :    LOG(INFO) << "Finalizing block-graph for \"" << input_module.value() << "\".";
 414    :  
 415  E :    std::vector<BlockGraphTransformInterface*> post_transforms;
 416  E :    pe::transforms::AddMetadataTransform add_metadata_tx(input_module);
 417    :    pe::transforms::AddPdbInfoTransform add_pdb_info_tx(output_pdb, 1,
 418  E :                                                        pdb_guid);
 419  E :    pe::transforms::PERemoveEmptySectionsTransform remove_empty_sections;
 420  E :    pe::transforms::PEPrepareHeadersTransform prep_headers_tx;
 421    :  
 422  E :    if (add_metadata)
 423  E :      post_transforms.push_back(&add_metadata_tx);
 424  E :    post_transforms.push_back(&add_pdb_info_tx);
 425  E :    post_transforms.push_back(&remove_empty_sections);
 426  E :    post_transforms.push_back(&prep_headers_tx);
 427    :  
 428    :    if (!block_graph::ApplyBlockGraphTransforms(post_transforms,
 429    :                                                policy,
 430    :                                                block_graph,
 431  E :                                                dos_header_block)) {
 432  i :      return false;
 433    :    }
 434    :  
 435  E :    return true;
 436  E :  }
 437    :  
 438    :  bool FinalizeOrderedBlockGraph(
 439    :      OrderedBlockGraph* ordered_block_graph,
 440  E :      BlockGraph::Block* dos_header_block) {
 441  E :    DCHECK_NE(reinterpret_cast<OrderedBlockGraph*>(NULL), ordered_block_graph);
 442  E :    DCHECK_NE(reinterpret_cast<BlockGraph::Block*>(NULL), dos_header_block);
 443  E :    pe::orderers::PEOrderer pe_orderer;
 444  E :    if (!pe_orderer.OrderBlockGraph(ordered_block_graph, dos_header_block))
 445  i :      return false;
 446  E :    return true;
 447  E :  }
 448    :  
 449    :  bool BuildImageLayout(size_t padding,
 450    :                        size_t code_alignment,
 451    :                        const OrderedBlockGraph& ordered_block_graph,
 452    :                        BlockGraph::Block* dos_header_block,
 453  E :                        ImageLayout* image_layout) {
 454  E :    DCHECK_NE(reinterpret_cast<BlockGraph::Block*>(NULL), dos_header_block);
 455  E :    DCHECK_NE(reinterpret_cast<ImageLayout*>(NULL), image_layout);
 456    :  
 457  E :    LOG(INFO) << "Building image layout.";
 458    :  
 459  E :    PEImageLayoutBuilder builder(image_layout);
 460  E :    builder.set_padding(padding);
 461  E :    builder.set_code_alignment(code_alignment);
 462  E :    if (!builder.LayoutImageHeaders(dos_header_block)) {
 463  i :      LOG(ERROR) << "PEImageLayoutBuilder::LayoutImageHeaders failed.";
 464  i :      return false;
 465    :    }
 466    :  
 467  E :    if (!builder.LayoutOrderedBlockGraph(ordered_block_graph)) {
 468  i :      LOG(ERROR) << "PEImageLayoutBuilder::LayoutOrderedBlockGraph failed.";
 469  i :      return false;
 470    :    }
 471    :  
 472  E :    LOG(INFO) << "Finalizing image layout.";
 473  E :    if (!builder.Finalize()) {
 474  i :      LOG(ERROR) << "PEImageLayoutBuilder::Finalize failed.";
 475  i :      return false;
 476    :    }
 477    :  
 478  E :    return true;
 479  E :  }
 480    :  
 481    :  void GetOmapRange(const std::vector<ImageLayout::SectionInfo>& sections,
 482  E :                    RelativeAddressRange* range) {
 483  E :    DCHECK_NE(reinterpret_cast<RelativeAddressRange*>(NULL), range);
 484    :  
 485    :    // There need to be at least two sections, one containing something and the
 486    :    // other containing the relocs.
 487  E :    DCHECK_GT(sections.size(), 1u);
 488  E :    DCHECK_EQ(sections.back().name, std::string(kRelocSectionName));
 489    :  
 490    :    // For some reason, if we output OMAP entries for the headers (before the
 491    :    // first section), everything falls apart. Not outputting these allows the
 492    :    // unittests to pass. Also, we don't want to output OMAP information for
 493    :    // the relocs, as these are entirely different from image to image.
 494  E :    RelativeAddress start_of_image = sections.front().addr;
 495  E :    RelativeAddress end_of_image = sections.back().addr;
 496  E :    *range = RelativeAddressRange(start_of_image, end_of_image - start_of_image);
 497  E :  }
 498    :  
 499    :  bool FinalizePdbFile(const base::FilePath input_module,
 500    :                       const base::FilePath output_module,
 501    :                       const RelativeAddressRange input_range,
 502    :                       const ImageLayout& image_layout,
 503    :                       const GUID& guid,
 504    :                       bool augment_pdb,
 505    :                       bool strip_strings,
 506    :                       bool compress_pdb,
 507  E :                       pdb::PdbFile* pdb_file) {
 508  E :    DCHECK(pdb_file != NULL);
 509    :  
 510  E :    LOG(INFO) << "Finalizing PDB file.";
 511    :  
 512  E :    VLOG(1) << "Updating GUID.";
 513  E :    if (!pdb::SetGuid(guid, pdb_file)) {
 514  i :      LOG(ERROR) << "Unable to set PDB GUID.";
 515  i :      return false;
 516    :    }
 517    :  
 518  E :    VLOG(1) << "Building OMAP vectors.";
 519  E :    std::vector<OMAP> omap_to, omap_from;
 520  E :    BuildOmapVectors(input_range, image_layout, &omap_to, &omap_from);
 521    :  
 522  E :    VLOG(1) << "Writing OMAP vectors.";
 523  E :    if (!pdb::SetOmapToStream(omap_to, pdb_file)) {
 524  i :      LOG(ERROR) << "Unable to set OMAP_TO.";
 525  i :      return false;
 526    :    }
 527  E :    if (!pdb::SetOmapFromStream(omap_from, pdb_file)) {
 528  i :      LOG(ERROR) << "Unable to set OMAP_FROM.";
 529  i :      return false;
 530    :    }
 531    :  
 532    :    // Parse the header and named streams.
 533  E :    pdb::PdbInfoHeader70 header = {};
 534  E :    pdb::NameStreamMap name_stream_map;
 535  E :    if (!pdb::ReadHeaderInfoStream(*pdb_file, &header, &name_stream_map))
 536  i :      return false;
 537    :  
 538    :    // Update/create the Syzygy history stream.
 539  E :    VLOG(1) << "Adding history stream to PDB.";
 540  E :    if (!WriteSyzygyHistoryStream(input_module, &name_stream_map, pdb_file))
 541  i :      return false;
 542    :  
 543    :    // Add redecomposition data in another stream, only if augment_pdb_ is set.
 544  E :    if (augment_pdb) {
 545  E :      PEFile new_pe_file;
 546  E :      if (!new_pe_file.Init(output_module)) {
 547  i :        LOG(ERROR) << "Failed to read newly written PE file.";
 548  i :        return false;
 549    :      }
 550    :  
 551  E :      VLOG(1) << "Adding serialized block-graph stream to PDB.";
 552    :      if (!WriteSyzygyBlockGraphStream(new_pe_file,
 553    :                                       image_layout,
 554    :                                       strip_strings,
 555    :                                       compress_pdb,
 556    :                                       &name_stream_map,
 557  E :                                       pdb_file)) {
 558  i :        return false;
 559    :      }
 560  E :    }
 561    :  
 562    :    // Write the updated name-stream map back to the header info stream.
 563  E :    VLOG(1) << "Updating PDB headers.";
 564  E :    if (!pdb::WriteHeaderInfoStream(header, name_stream_map, pdb_file))
 565  i :      return false;
 566    :  
 567    :    // Stream 0 contains a copy of the previous PDB's directory. This, combined
 568    :    // with copy-on-write semantics of individual blocks makes the file contain
 569    :    // its whole edit history. Since we're writing a 'new' PDB file (we reset the
 570    :    // GUID and age), we have no history so can safely throw away this stream.
 571  E :    VLOG(1) << "Removing previous PDB directory stream.";
 572  E :    pdb_file->ReplaceStream(0, NULL);
 573    :  
 574  E :    return true;
 575  E :  }
 576    :  
 577    :  }  // namespace pe

Coverage information generated Thu Jan 14 17:40:38 2016.