| 1837 | } |
| 1838 | |
| 1839 | void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal) |
| 1840 | { |
| 1841 | auto beType = constVal->GetType(); |
| 1842 | if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constVal)) |
| 1843 | { |
| 1844 | auto sym = GetSymbol(globalVar); |
| 1845 | |
| 1846 | BeMCRelocation reloc; |
| 1847 | reloc.mKind = BeMCRelocationKind_ADDR64; |
| 1848 | reloc.mOffset = sect.mData.GetPos(); |
| 1849 | reloc.mSymTableIdx = sym->mIdx; |
| 1850 | sect.mRelocs.push_back(reloc); |
| 1851 | sect.mData.Write((int64)0); |
| 1852 | } |
| 1853 | else if (auto beFunc = BeValueDynCast<BeFunction>(constVal)) |
| 1854 | { |
| 1855 | auto sym = GetSymbol(beFunc); |
| 1856 | |
| 1857 | BeMCRelocation reloc; |
| 1858 | reloc.mKind = BeMCRelocationKind_ADDR64; |
| 1859 | reloc.mOffset = sect.mData.GetPos(); |
| 1860 | reloc.mSymTableIdx = sym->mIdx; |
| 1861 | sect.mRelocs.push_back(reloc); |
| 1862 | sect.mData.Write((int64)0); |
| 1863 | } |
| 1864 | else if (auto constStruct = BeValueDynCast<BeStructConstant>(constVal)) |
| 1865 | { |
| 1866 | int startOfs = sect.mData.GetSize(); |
| 1867 | if (constStruct->mType->mTypeCode == BeTypeCode_Struct) |
| 1868 | { |
| 1869 | BeStructType* structType = (BeStructType*)constStruct->mType; |
| 1870 | BF_ASSERT(structType->mMembers.size() == constStruct->mMemberValues.size()); |
| 1871 | for (int memberIdx = 0; memberIdx < (int)constStruct->mMemberValues.size(); memberIdx++) |
| 1872 | { |
| 1873 | auto& member = structType->mMembers[memberIdx]; |
| 1874 | // Do any per-member alignment |
| 1875 | sect.mData.WriteZeros(member.mByteOffset - (sect.mData.GetSize() - startOfs)); |
| 1876 | WriteConst(sect, constStruct->mMemberValues[memberIdx]); |
| 1877 | } |
| 1878 | // Do end padding |
| 1879 | sect.mData.WriteZeros(structType->mSize - (sect.mData.GetSize() - startOfs)); |
| 1880 | } |
| 1881 | else if (constStruct->mType->mTypeCode == BeTypeCode_SizedArray) |
| 1882 | { |
| 1883 | BeSizedArrayType* arrayType = (BeSizedArrayType*)constStruct->mType; |
| 1884 | for (auto& memberVal : constStruct->mMemberValues) |
| 1885 | { |
| 1886 | WriteConst(sect, memberVal); |
| 1887 | int padding = arrayType->mElementType->GetStride() - arrayType->mElementType->mSize; |
| 1888 | if (padding > 0) |
| 1889 | sect.mData.WriteZeros(padding); |
| 1890 | } |
| 1891 | } |
| 1892 | else |
| 1893 | BF_FATAL("Invalid StructConst type"); |
| 1894 | } |
| 1895 | else if (auto constStr = BeValueDynCast<BeStringConstant>(constVal)) |
| 1896 | { |
nothing calls this directly
no test coverage detected