1/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BCC_CACHEWRITER_H
18#define BCC_CACHEWRITER_H
19
20#include <bcc/bcc_cache.h>
21
22#include "FileHandle.h"
23
24#include <map>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace bcc {
30  class Script;
31
32  class CacheWriter {
33  private:
34    Script *mpOwner;
35
36    FileHandle *mObjFile;
37    FileHandle *mInfoFile;
38
39    std::vector<std::pair<char const *, size_t> > mStringPool;
40
41    std::map<std::string,
42             std::pair<uint32_t, unsigned char const *> > mDependencies;
43
44    OBCC_Header *mpHeaderSection;
45    OBCC_StringPool *mpStringPoolSection;
46    OBCC_DependencyTable *mpDependencyTableSection;
47    //OBCC_RelocationTable *mpRelocationTableSection;
48    OBCC_ExportVarList *mpExportVarListSection;
49    OBCC_ExportFuncList *mpExportFuncListSection;
50    OBCC_PragmaList *mpPragmaListSection;
51    OBCC_FuncTable *mpFuncTableSection;
52    OBCC_ObjectSlotList *mpObjectSlotSection;
53
54  public:
55    CacheWriter()
56      : mpHeaderSection(NULL), mpStringPoolSection(NULL),
57        mpDependencyTableSection(NULL), mpExportVarListSection(NULL),
58        mpExportFuncListSection(NULL), mpPragmaListSection(NULL),
59        mpFuncTableSection(NULL), mpObjectSlotSection(NULL) {
60    }
61
62    ~CacheWriter();
63
64    bool writeCacheFile(FileHandle *objFile,
65                        FileHandle *infoFile,
66                        Script *S,
67                        uint32_t libRS_threadable);
68
69    void addDependency(OBCC_ResourceType resType,
70                       std::string const &resName,
71                       unsigned char const *sha1) {
72      mDependencies.insert(std::make_pair(resName,
73                           std::make_pair((uint32_t)resType, sha1)));
74    }
75
76  private:
77    bool prepareHeader(uint32_t libRS_threadable);
78    bool prepareStringPool();
79    bool prepareDependencyTable();
80    bool prepareRelocationTable();
81    bool prepareExportVarList();
82    bool prepareExportFuncList();
83    bool preparePragmaList();
84    bool prepareFuncTable();
85    bool prepareObjectSlotList();
86
87    bool writeAll();
88
89    bool calcSectionOffset();
90    bool calcContextChecksum();
91
92    size_t addString(char const *str, size_t size) {
93      mStringPool.push_back(std::make_pair(str, size));
94      return mStringPool.size() - 1;
95    }
96
97  };
98
99} // namespace bcc
100
101#endif // BCC_CACHEWRITER_H
102