1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_soltools.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir /* 32*cdf0e10cSrcweir * adjustvisibilty -- a tool to adjust the visibility of the so called 33*cdf0e10cSrcweir * 'fix and continue' globalized symbols generated by 34*cdf0e10cSrcweir * the Sun Studio 8 compiler from 'DEFAULT' to 'HIDDEN' 35*cdf0e10cSrcweir * 36*cdf0e10cSrcweir * References: "Linker and Libraries Guide", Solaris 9 documentation 37*cdf0e10cSrcweir * "Stabs Interface", SunStudio 8 documentation 38*cdf0e10cSrcweir */ 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <string> 41*cdf0e10cSrcweir #include <iostream> 42*cdf0e10cSrcweir #include <exception> 43*cdf0e10cSrcweir #include <stdexcept> 44*cdf0e10cSrcweir #include <cerrno> 45*cdf0e10cSrcweir #include <fcntl.h> 46*cdf0e10cSrcweir #include <unistd.h> 47*cdf0e10cSrcweir #include <libelf.h> 48*cdf0e10cSrcweir #include <gelf.h> 49*cdf0e10cSrcweir #include <utime.h> 50*cdf0e10cSrcweir #include <sys/types.h> 51*cdf0e10cSrcweir #include <sys/stat.h> 52*cdf0e10cSrcweir #include <limits> 53*cdf0e10cSrcweir #include <stdio.h> 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir // Note: There is no GELF_ST_VISIBILITY macro in gelf.h, we roll our own. 56*cdf0e10cSrcweir #define GELF_ST_VISIBILITY(o) ((o)&0x3) // See "Linker and Libraries Guide". 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir // See "Linker and Libraries Guide", ELF object format description. 59*cdf0e10cSrcweir static const char* SymbolType[STT_NUM] = { 60*cdf0e10cSrcweir "NOTYPE", 61*cdf0e10cSrcweir "OBJECT", 62*cdf0e10cSrcweir "FUNC ", 63*cdf0e10cSrcweir "SECT ", 64*cdf0e10cSrcweir "FILE ", 65*cdf0e10cSrcweir "COMM ", 66*cdf0e10cSrcweir "TLS " 67*cdf0e10cSrcweir }; 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir static const char* SymbolBinding[STB_NUM] = { 70*cdf0e10cSrcweir "LOCAL ", 71*cdf0e10cSrcweir "GLOBAL", 72*cdf0e10cSrcweir "WEAK " 73*cdf0e10cSrcweir }; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir static const char* SymbolVisibility[4] = { // Note: There is no STV_NUM macro 76*cdf0e10cSrcweir "DEFAULT ", 77*cdf0e10cSrcweir "INTERNAL ", 78*cdf0e10cSrcweir "HIDDEN ", 79*cdf0e10cSrcweir "PROTECTED" 80*cdf0e10cSrcweir }; 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir class ElfError : public std::exception 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir public: 85*cdf0e10cSrcweir ElfError(const std::string& rFile, const std::string& rMessage); 86*cdf0e10cSrcweir ~ElfError() throw() {}; 87*cdf0e10cSrcweir virtual const char* what() const throw() { return m_sMessage.c_str(); } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir private: 90*cdf0e10cSrcweir std::string m_sMessage; 91*cdf0e10cSrcweir }; 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir ElfError::ElfError(const std::string& rFile, const std::string& rMessage) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir if ( rFile != "" ) { 96*cdf0e10cSrcweir m_sMessage = rFile; 97*cdf0e10cSrcweir m_sMessage += ": "; 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir m_sMessage += rMessage; 100*cdf0e10cSrcweir const char *pElfMsg = elf_errmsg(0); 101*cdf0e10cSrcweir if ( pElfMsg ) { 102*cdf0e10cSrcweir m_sMessage += ": "; 103*cdf0e10cSrcweir m_sMessage += pElfMsg; 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir void initElfLib() 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir if ( elf_version(EV_CURRENT) == EV_NONE) { 110*cdf0e10cSrcweir throw ElfError("", "elf_version() failed"); 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir return; 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir bool isFixAndContinueSymbol(const std::string& rSymbol) 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir // The globalized 'fix and continue' symbols have the following 118*cdf0e10cSrcweir // form, see "Stabs interface", page 164: 119*cdf0e10cSrcweir // {.$}X{ABC}uniquepattern[.function_name][EQUIVn][.variable_name] 120*cdf0e10cSrcweir char c0 = rSymbol[0]; 121*cdf0e10cSrcweir char c1 = rSymbol[1]; 122*cdf0e10cSrcweir char c2 = rSymbol[2]; 123*cdf0e10cSrcweir if ( c0 == '.' || c0 == '$' ) { 124*cdf0e10cSrcweir if ( c1 == 'X' ) { 125*cdf0e10cSrcweir if ( c2 == 'A' || c2 == 'B' || c2 == 'C' || c2 == 'D' ) { 126*cdf0e10cSrcweir return true; 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir return false; 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir void adjustVisibility( const std::string& rFile, int fd, bool bVerbose) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir if ( bVerbose ) { 136*cdf0e10cSrcweir std::cout << "File: " << rFile << ": adjusting 'fix and continue' symbol visibility\n"; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir try { 140*cdf0e10cSrcweir Elf* pElf; 141*cdf0e10cSrcweir if ((pElf = elf_begin(fd, ELF_C_RDWR, 0)) == NULL) { 142*cdf0e10cSrcweir throw ElfError(rFile, "elf_begin() failed"); 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir // Check if file is ELF file. 145*cdf0e10cSrcweir if ( elf_kind(pElf) != ELF_K_ELF ) { 146*cdf0e10cSrcweir throw ElfError(rFile, "elf_kind() failed, file is not an ELF object file"); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir // Iterate over sections. 150*cdf0e10cSrcweir Elf_Scn* pScn = 0; 151*cdf0e10cSrcweir while ( (pScn = elf_nextscn(pElf, pScn)) != 0 ) { 152*cdf0e10cSrcweir GElf_Shdr aShdr; 153*cdf0e10cSrcweir if ( gelf_getshdr(pScn, &aShdr) == 0 ) { 154*cdf0e10cSrcweir throw ElfError(rFile, "gelf_getshdr() failed"); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir if ( aShdr.sh_type != SHT_SYMTAB ) { 157*cdf0e10cSrcweir continue; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir // Section is a symbol section. Get the assiociated data. 160*cdf0e10cSrcweir Elf_Data* pSymbolData; 161*cdf0e10cSrcweir if ( (pSymbolData = elf_getdata(pScn, 0)) == NULL ) { 162*cdf0e10cSrcweir throw ElfError(rFile, "elf_getdata() failed"); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir // Iterate over symbol table. 165*cdf0e10cSrcweir GElf_Xword nSymbols = aShdr.sh_size / aShdr.sh_entsize; 166*cdf0e10cSrcweir if ( nSymbols > std::numeric_limits< int >::max() ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir throw ElfError(rFile, "too many symbols"); 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir for ( int nIndex = 0; nIndex < nSymbols; ++nIndex) { 171*cdf0e10cSrcweir // Get symbol. 172*cdf0e10cSrcweir GElf_Sym aSymbol; 173*cdf0e10cSrcweir if ( gelf_getsym(pSymbolData, nIndex, &aSymbol) == NULL ) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir throw ElfError(rFile, "gelf_getsym() failed"); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir std::string sSymbolName(elf_strptr(pElf, aShdr.sh_link, aSymbol.st_name)); 178*cdf0e10cSrcweir if ( isFixAndContinueSymbol(sSymbolName) ) { 179*cdf0e10cSrcweir // Get the symbol visibility. 180*cdf0e10cSrcweir unsigned int nSymbolVisibility = GELF_ST_VISIBILITY(aSymbol.st_other); 181*cdf0e10cSrcweir if ( bVerbose ) { 182*cdf0e10cSrcweir // Get the symbol type and binding. 183*cdf0e10cSrcweir unsigned int nSymbolType = GELF_ST_TYPE(aSymbol.st_info); 184*cdf0e10cSrcweir unsigned int nSymbolBind = GELF_ST_BIND(aSymbol.st_info); 185*cdf0e10cSrcweir std::cout << "Symbol: " << sSymbolName << ", " 186*cdf0e10cSrcweir << "Type: "; 187*cdf0e10cSrcweir if ( SymbolType[nSymbolType] ) { 188*cdf0e10cSrcweir std::cout << SymbolType[nSymbolType]; 189*cdf0e10cSrcweir } else { 190*cdf0e10cSrcweir std::cout << nSymbolType; 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir std::cout << ", Binding: "; 193*cdf0e10cSrcweir if ( SymbolBinding[nSymbolBind] ) { 194*cdf0e10cSrcweir std::cout << SymbolBinding[nSymbolBind]; 195*cdf0e10cSrcweir } else { 196*cdf0e10cSrcweir std::cout << nSymbolBind; 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir std::cout << ", Visibility: "; 199*cdf0e10cSrcweir if ( SymbolVisibility[nSymbolVisibility] ) { 200*cdf0e10cSrcweir std::cout << SymbolVisibility[nSymbolVisibility]; 201*cdf0e10cSrcweir } else { 202*cdf0e10cSrcweir std::cout << nSymbolVisibility; 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir std::cout << "-> " << SymbolVisibility[STV_HIDDEN] << "\n"; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir // Toggle visibility to "hidden". 207*cdf0e10cSrcweir aSymbol.st_other = GELF_ST_VISIBILITY(STV_HIDDEN); 208*cdf0e10cSrcweir // Write back symbol data to underlying structure. 209*cdf0e10cSrcweir if ( gelf_update_sym(pSymbolData, nIndex, &aSymbol) == NULL ) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir throw ElfError(rFile, "gelf_update_sym() failed"); 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir // Write changed object file to disk. 217*cdf0e10cSrcweir if ( elf_update(pElf, ELF_C_WRITE) == -1 ) { 218*cdf0e10cSrcweir throw ElfError(rFile, "elf_update() failed"); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir elf_end(pElf); 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir } catch (ElfError& e) { 223*cdf0e10cSrcweir close(fd); 224*cdf0e10cSrcweir throw; 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir return; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir void processObject(const std::string& rFile, bool bPreserve, bool bVerbose) 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir int fd; 232*cdf0e10cSrcweir struct stat aStatBuf; 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir if ((fd = open(rFile.c_str(), O_RDWR)) == -1) { 235*cdf0e10cSrcweir std::string sMessage("adjustVisibilty() failed: can't open file "); 236*cdf0e10cSrcweir sMessage += rFile; 237*cdf0e10cSrcweir sMessage += ": "; 238*cdf0e10cSrcweir sMessage += std::strerror(errno); 239*cdf0e10cSrcweir throw std::runtime_error(sMessage); 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir if ( bPreserve ) { 243*cdf0e10cSrcweir if ( fstat(fd, &aStatBuf) == -1) { 244*cdf0e10cSrcweir std::string sMessage("adjustVisibilty() failed: can't stat file "); 245*cdf0e10cSrcweir sMessage += rFile; 246*cdf0e10cSrcweir sMessage += ": "; 247*cdf0e10cSrcweir sMessage += std::strerror(errno); 248*cdf0e10cSrcweir throw std::runtime_error(sMessage); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir } 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir adjustVisibility(rFile, fd, bVerbose); 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir close(fd); 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir if ( bPreserve ) { 257*cdf0e10cSrcweir struct utimbuf aUtimBuf = {aStatBuf.st_atime, aStatBuf.st_mtime}; 258*cdf0e10cSrcweir if ( utime(rFile.c_str(), &aUtimBuf) == -1 ) { 259*cdf0e10cSrcweir std::string sMessage("adjustVisibilty() failed: can't reset timestamp "); 260*cdf0e10cSrcweir sMessage += rFile; 261*cdf0e10cSrcweir sMessage += ": "; 262*cdf0e10cSrcweir sMessage += std::strerror(errno); 263*cdf0e10cSrcweir throw std::runtime_error(sMessage); 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir } 266*cdf0e10cSrcweir return; 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir int main(int argc, char* argv[]) 270*cdf0e10cSrcweir { 271*cdf0e10cSrcweir int c; 272*cdf0e10cSrcweir bool bPreserve = false; 273*cdf0e10cSrcweir bool bVerbose = false; 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir while ( (c = getopt(argc, argv, "pv")) != -1 ) { 276*cdf0e10cSrcweir switch(c) { 277*cdf0e10cSrcweir case 'p': 278*cdf0e10cSrcweir bPreserve = true; 279*cdf0e10cSrcweir break; 280*cdf0e10cSrcweir case 'v': 281*cdf0e10cSrcweir bVerbose = true; 282*cdf0e10cSrcweir break; 283*cdf0e10cSrcweir case '?': 284*cdf0e10cSrcweir std::cerr << "Unrecognized option: -" << optopt << "\n"; 285*cdf0e10cSrcweir break; 286*cdf0e10cSrcweir default: 287*cdf0e10cSrcweir break; 288*cdf0e10cSrcweir } 289*cdf0e10cSrcweir } 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir if ( optind == argc ) { 292*cdf0e10cSrcweir std::cout << "usage: " << argv[0] << " [-pv] <elf-object> ...\n"; 293*cdf0e10cSrcweir std::cout << " -p preserve time stamps\n"; 294*cdf0e10cSrcweir std::cout << " -v verbose\n"; 295*cdf0e10cSrcweir return 1; 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir try { 299*cdf0e10cSrcweir initElfLib(); 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir for ( ; optind < argc; optind++ ) { 302*cdf0e10cSrcweir processObject(std::string(argv[optind]), bPreserve, bVerbose); 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir } catch (std::exception& e) { 306*cdf0e10cSrcweir std::cerr << argv[0] << ": " << e.what() << "\n"; 307*cdf0e10cSrcweir return 1; 308*cdf0e10cSrcweir } 309*cdf0e10cSrcweir 310*cdf0e10cSrcweir return 0; 311*cdf0e10cSrcweir } 312