Index: openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h =================================================================== --- openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h +++ openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.h @@ -96,6 +96,12 @@ const ELF64LEObjectFile * getOrCreateELFObjectFile(const GenericDeviceTy &Device, DeviceImageTy &Image); + /// Extract the global's information from the ELF image, section, and symbol. + virtual Error getGlobalMetadataFromELF(const DeviceImageTy &Image, + const ELF64LE::Sym &Symbol, + const ELF64LE::Shdr &Section, + GlobalTy &ImageGlobal); + /// Actually move memory between host and device. See readGlobalFromDevice and /// writeGlobalToDevice for the interface description. Error moveGlobalBetweenDeviceAndHost(GenericDeviceTy &Device, Index: openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp =================================================================== --- openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp +++ openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp @@ -46,6 +46,21 @@ return &Result.first->second; } +Error GenericGlobalHandlerTy::getGlobalMetadataFromELF( + const DeviceImageTy &Image, const ELF64LE::Sym &Symbol, + const ELF64LE::Shdr &Section, GlobalTy &ImageGlobal) { + + // The global's address is computed as the image begin + the ELF section + // offset + the ELF symbol value. + ImageGlobal.setPtr((char *)Image.getStart() + Section.sh_offset + + Symbol.st_value); + + // Set the global's size. + ImageGlobal.setSize(Symbol.st_size); + + return Plugin::success(); +} + Error GenericGlobalHandlerTy::moveGlobalBetweenDeviceAndHost( GenericDeviceTy &Device, DeviceImageTy &Image, const GlobalTy &HostGlobal, bool Device2Host) { @@ -111,19 +126,14 @@ ImageGlobal.getName().data()); // Get the section to which the symbol belongs. - auto SymSecOrErr = ELFObj->getELFFile().getSection((*SymOrErr)->st_shndx); - if (!SymSecOrErr) + auto SecOrErr = ELFObj->getELFFile().getSection((*SymOrErr)->st_shndx); + if (!SecOrErr) return Plugin::error("Failed to get ELF section from global '%s': %s", ImageGlobal.getName().data(), - toString(SymOrErr.takeError()).data()); + toString(SecOrErr.takeError()).data()); - // Save the global symbol's address and size. The address of the global is the - // image base address + the section offset + the symbol value. - ImageGlobal.setPtr((char *)Image.getStart() + (*SymSecOrErr)->sh_offset + - (*SymOrErr)->st_value); - ImageGlobal.setSize((*SymOrErr)->st_size); - - return Plugin::success(); + // Setup the global symbol's address and size. + return getGlobalMetadataFromELF(Image, **SymOrErr, **SecOrErr, ImageGlobal); } Error GenericGlobalHandlerTy::readGlobalFromImage(GenericDeviceTy &Device,