1cac9c5f9SGreg Clayton //===-- SBSection.cpp -------------------------------------------*- C++ -*-===// 2cac9c5f9SGreg Clayton // 3cac9c5f9SGreg Clayton // The LLVM Compiler Infrastructure 4cac9c5f9SGreg Clayton // 5cac9c5f9SGreg Clayton // This file is distributed under the University of Illinois Open Source 6cac9c5f9SGreg Clayton // License. See LICENSE.TXT for details. 7cac9c5f9SGreg Clayton // 8cac9c5f9SGreg Clayton //===----------------------------------------------------------------------===// 9cac9c5f9SGreg Clayton 10cac9c5f9SGreg Clayton #include "lldb/API/SBSection.h" 11cac9c5f9SGreg Clayton #include "lldb/API/SBStream.h" 12cac9c5f9SGreg Clayton #include "lldb/Core/DataBuffer.h" 13cac9c5f9SGreg Clayton #include "lldb/Core/DataExtractor.h" 14cac9c5f9SGreg Clayton #include "lldb/Core/Log.h" 15cac9c5f9SGreg Clayton #include "lldb/Core/Module.h" 16d9dc52dcSGreg Clayton #include "lldb/Core/Section.h" 17d9dc52dcSGreg Clayton #include "lldb/Core/StreamString.h" 18cac9c5f9SGreg Clayton 19cac9c5f9SGreg Clayton 20cac9c5f9SGreg Clayton using namespace lldb; 21cac9c5f9SGreg Clayton using namespace lldb_private; 22cac9c5f9SGreg Clayton 23cac9c5f9SGreg Clayton 24cac9c5f9SGreg Clayton SBSection::SBSection () : 25*e72dfb32SGreg Clayton m_opaque_wp () 26cac9c5f9SGreg Clayton { 27cac9c5f9SGreg Clayton } 28cac9c5f9SGreg Clayton 29cac9c5f9SGreg Clayton SBSection::SBSection (const SBSection &rhs) : 30*e72dfb32SGreg Clayton m_opaque_wp (rhs.m_opaque_wp) 31cac9c5f9SGreg Clayton { 32cac9c5f9SGreg Clayton } 33cac9c5f9SGreg Clayton 34cac9c5f9SGreg Clayton 35cac9c5f9SGreg Clayton 36*e72dfb32SGreg Clayton SBSection::SBSection (const lldb::SectionSP §ion_sp) : 37*e72dfb32SGreg Clayton m_opaque_wp () // Don't init with section_sp otherwise this will throw if section_sp doesn't contain a valid Section * 38cac9c5f9SGreg Clayton { 39*e72dfb32SGreg Clayton if (section_sp) 40*e72dfb32SGreg Clayton m_opaque_wp = section_sp; 41cac9c5f9SGreg Clayton } 42cac9c5f9SGreg Clayton 43cac9c5f9SGreg Clayton const SBSection & 44cac9c5f9SGreg Clayton SBSection::operator = (const SBSection &rhs) 45cac9c5f9SGreg Clayton { 46*e72dfb32SGreg Clayton m_opaque_wp = rhs.m_opaque_wp; 47cac9c5f9SGreg Clayton return *this; 48cac9c5f9SGreg Clayton } 49cac9c5f9SGreg Clayton 50cac9c5f9SGreg Clayton SBSection::~SBSection () 51cac9c5f9SGreg Clayton { 52cac9c5f9SGreg Clayton } 53cac9c5f9SGreg Clayton 54cac9c5f9SGreg Clayton bool 55cac9c5f9SGreg Clayton SBSection::IsValid () const 56cac9c5f9SGreg Clayton { 57*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 58*e72dfb32SGreg Clayton return section_sp && section_sp->GetModule().get() != NULL; 59cac9c5f9SGreg Clayton } 60cac9c5f9SGreg Clayton 61f644ddf4SGreg Clayton const char * 62f644ddf4SGreg Clayton SBSection::GetName () 63f644ddf4SGreg Clayton { 64*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 65*e72dfb32SGreg Clayton if (section_sp) 66*e72dfb32SGreg Clayton return section_sp->GetName().GetCString(); 67f644ddf4SGreg Clayton return NULL; 68f644ddf4SGreg Clayton } 69f644ddf4SGreg Clayton 70f644ddf4SGreg Clayton 71cac9c5f9SGreg Clayton lldb::SBSection 72cac9c5f9SGreg Clayton SBSection::FindSubSection (const char *sect_name) 73cac9c5f9SGreg Clayton { 74cac9c5f9SGreg Clayton lldb::SBSection sb_section; 75*e72dfb32SGreg Clayton if (sect_name) 76*e72dfb32SGreg Clayton { 77*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 78*e72dfb32SGreg Clayton if (section_sp) 79cac9c5f9SGreg Clayton { 80cac9c5f9SGreg Clayton ConstString const_sect_name(sect_name); 81*e72dfb32SGreg Clayton sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name)); 82*e72dfb32SGreg Clayton } 83cac9c5f9SGreg Clayton } 84cac9c5f9SGreg Clayton return sb_section; 85cac9c5f9SGreg Clayton } 86cac9c5f9SGreg Clayton 87cac9c5f9SGreg Clayton size_t 88cac9c5f9SGreg Clayton SBSection::GetNumSubSections () 89cac9c5f9SGreg Clayton { 90*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 91*e72dfb32SGreg Clayton if (section_sp) 92*e72dfb32SGreg Clayton return section_sp->GetChildren ().GetSize(); 93cac9c5f9SGreg Clayton return 0; 94cac9c5f9SGreg Clayton } 95cac9c5f9SGreg Clayton 96cac9c5f9SGreg Clayton lldb::SBSection 97cac9c5f9SGreg Clayton SBSection::GetSubSectionAtIndex (size_t idx) 98cac9c5f9SGreg Clayton { 99cac9c5f9SGreg Clayton lldb::SBSection sb_section; 100*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 101*e72dfb32SGreg Clayton if (section_sp) 102*e72dfb32SGreg Clayton sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx)); 103cac9c5f9SGreg Clayton return sb_section; 104cac9c5f9SGreg Clayton } 105cac9c5f9SGreg Clayton 106*e72dfb32SGreg Clayton lldb::SectionSP 107*e72dfb32SGreg Clayton SBSection::GetSP() const 108cac9c5f9SGreg Clayton { 109*e72dfb32SGreg Clayton return m_opaque_wp.lock(); 110cac9c5f9SGreg Clayton } 111cac9c5f9SGreg Clayton 112cac9c5f9SGreg Clayton void 113*e72dfb32SGreg Clayton SBSection::SetSP(const lldb::SectionSP §ion_sp) 114cac9c5f9SGreg Clayton { 115*e72dfb32SGreg Clayton m_opaque_wp = section_sp; 116cac9c5f9SGreg Clayton } 117cac9c5f9SGreg Clayton 118cac9c5f9SGreg Clayton lldb::addr_t 119cac9c5f9SGreg Clayton SBSection::GetFileAddress () 120cac9c5f9SGreg Clayton { 121cac9c5f9SGreg Clayton lldb::addr_t file_addr = LLDB_INVALID_ADDRESS; 122*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 123*e72dfb32SGreg Clayton if (section_sp) 124*e72dfb32SGreg Clayton return section_sp->GetFileAddress(); 125cac9c5f9SGreg Clayton return file_addr; 126cac9c5f9SGreg Clayton } 127cac9c5f9SGreg Clayton 128cac9c5f9SGreg Clayton lldb::addr_t 129cac9c5f9SGreg Clayton SBSection::GetByteSize () 130cac9c5f9SGreg Clayton { 131*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 132*e72dfb32SGreg Clayton if (section_sp) 133*e72dfb32SGreg Clayton return section_sp->GetByteSize(); 134cac9c5f9SGreg Clayton return 0; 135cac9c5f9SGreg Clayton } 136cac9c5f9SGreg Clayton 137cac9c5f9SGreg Clayton uint64_t 138cac9c5f9SGreg Clayton SBSection::GetFileOffset () 139cac9c5f9SGreg Clayton { 140*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 141*e72dfb32SGreg Clayton if (section_sp) 142cac9c5f9SGreg Clayton { 143*e72dfb32SGreg Clayton ModuleSP module_sp (section_sp->GetModule()); 144*e72dfb32SGreg Clayton if (module_sp) 145cac9c5f9SGreg Clayton { 146*e72dfb32SGreg Clayton ObjectFile *objfile = module_sp->GetObjectFile(); 147cac9c5f9SGreg Clayton if (objfile) 148*e72dfb32SGreg Clayton return objfile->GetOffset() + section_sp->GetFileOffset(); 149cac9c5f9SGreg Clayton } 150cac9c5f9SGreg Clayton } 151*e72dfb32SGreg Clayton return UINT64_MAX; 152cac9c5f9SGreg Clayton } 153cac9c5f9SGreg Clayton 154cac9c5f9SGreg Clayton uint64_t 155cac9c5f9SGreg Clayton SBSection::GetFileByteSize () 156cac9c5f9SGreg Clayton { 157*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 158*e72dfb32SGreg Clayton if (section_sp) 159*e72dfb32SGreg Clayton return section_sp->GetFileSize(); 160cac9c5f9SGreg Clayton return 0; 161cac9c5f9SGreg Clayton } 162cac9c5f9SGreg Clayton 163cac9c5f9SGreg Clayton SBData 164d9dc52dcSGreg Clayton SBSection::GetSectionData () 165d9dc52dcSGreg Clayton { 166d9dc52dcSGreg Clayton return GetSectionData (0, UINT64_MAX); 167d9dc52dcSGreg Clayton } 168d9dc52dcSGreg Clayton 169d9dc52dcSGreg Clayton SBData 170cac9c5f9SGreg Clayton SBSection::GetSectionData (uint64_t offset, uint64_t size) 171cac9c5f9SGreg Clayton { 172cac9c5f9SGreg Clayton SBData sb_data; 173*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 174*e72dfb32SGreg Clayton if (section_sp) 175cac9c5f9SGreg Clayton { 176*e72dfb32SGreg Clayton const uint64_t sect_file_size = section_sp->GetFileSize(); 177cac9c5f9SGreg Clayton if (sect_file_size > 0) 178cac9c5f9SGreg Clayton { 179*e72dfb32SGreg Clayton ModuleSP module_sp (section_sp->GetModule()); 180*e72dfb32SGreg Clayton if (module_sp) 181cac9c5f9SGreg Clayton { 182*e72dfb32SGreg Clayton ObjectFile *objfile = module_sp->GetObjectFile(); 183cac9c5f9SGreg Clayton if (objfile) 184cac9c5f9SGreg Clayton { 185*e72dfb32SGreg Clayton const uint64_t sect_file_offset = objfile->GetOffset() + section_sp->GetFileOffset(); 186cac9c5f9SGreg Clayton const uint64_t file_offset = sect_file_offset + offset; 187cac9c5f9SGreg Clayton uint64_t file_size = size; 188cac9c5f9SGreg Clayton if (file_size == UINT64_MAX) 189cac9c5f9SGreg Clayton { 190*e72dfb32SGreg Clayton file_size = section_sp->GetByteSize(); 191cac9c5f9SGreg Clayton if (file_size > offset) 192cac9c5f9SGreg Clayton file_size -= offset; 193cac9c5f9SGreg Clayton else 194cac9c5f9SGreg Clayton file_size = 0; 195cac9c5f9SGreg Clayton } 196cac9c5f9SGreg Clayton DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size)); 197cac9c5f9SGreg Clayton if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) 198cac9c5f9SGreg Clayton { 199cac9c5f9SGreg Clayton DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp, 200cac9c5f9SGreg Clayton objfile->GetByteOrder(), 201cac9c5f9SGreg Clayton objfile->GetAddressByteSize())); 202cac9c5f9SGreg Clayton 203cac9c5f9SGreg Clayton sb_data.SetOpaque (data_extractor_sp); 204cac9c5f9SGreg Clayton } 205cac9c5f9SGreg Clayton } 206cac9c5f9SGreg Clayton } 207cac9c5f9SGreg Clayton } 208cac9c5f9SGreg Clayton } 209cac9c5f9SGreg Clayton return sb_data; 210cac9c5f9SGreg Clayton } 211cac9c5f9SGreg Clayton 212cac9c5f9SGreg Clayton SectionType 213cac9c5f9SGreg Clayton SBSection::GetSectionType () 214cac9c5f9SGreg Clayton { 215*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 216*e72dfb32SGreg Clayton if (section_sp.get()) 217*e72dfb32SGreg Clayton return section_sp->GetType(); 218cac9c5f9SGreg Clayton return eSectionTypeInvalid; 219cac9c5f9SGreg Clayton } 220cac9c5f9SGreg Clayton 221cac9c5f9SGreg Clayton 222cac9c5f9SGreg Clayton bool 223cac9c5f9SGreg Clayton SBSection::operator == (const SBSection &rhs) 224cac9c5f9SGreg Clayton { 225*e72dfb32SGreg Clayton SectionSP lhs_section_sp (GetSP()); 226*e72dfb32SGreg Clayton SectionSP rhs_section_sp (rhs.GetSP()); 227*e72dfb32SGreg Clayton if (lhs_section_sp && rhs_section_sp) 228*e72dfb32SGreg Clayton return lhs_section_sp == rhs_section_sp; 229cac9c5f9SGreg Clayton return false; 230cac9c5f9SGreg Clayton } 231cac9c5f9SGreg Clayton 232cac9c5f9SGreg Clayton bool 233cac9c5f9SGreg Clayton SBSection::operator != (const SBSection &rhs) 234cac9c5f9SGreg Clayton { 235*e72dfb32SGreg Clayton SectionSP lhs_section_sp (GetSP()); 236*e72dfb32SGreg Clayton SectionSP rhs_section_sp (rhs.GetSP()); 237*e72dfb32SGreg Clayton return lhs_section_sp != rhs_section_sp; 238cac9c5f9SGreg Clayton } 239cac9c5f9SGreg Clayton 240cac9c5f9SGreg Clayton bool 241cac9c5f9SGreg Clayton SBSection::GetDescription (SBStream &description) 242cac9c5f9SGreg Clayton { 243da7bc7d0SGreg Clayton Stream &strm = description.ref(); 244da7bc7d0SGreg Clayton 245*e72dfb32SGreg Clayton SectionSP section_sp (GetSP()); 246*e72dfb32SGreg Clayton if (section_sp) 247cac9c5f9SGreg Clayton { 248*e72dfb32SGreg Clayton const addr_t file_addr = section_sp->GetFileAddress(); 249*e72dfb32SGreg Clayton strm.Printf ("[0x%16.16llx-0x%16.16llx) ", file_addr, file_addr + section_sp->GetByteSize()); 250*e72dfb32SGreg Clayton section_sp->DumpName(&strm); 251cac9c5f9SGreg Clayton } 252cac9c5f9SGreg Clayton else 253cac9c5f9SGreg Clayton { 254da7bc7d0SGreg Clayton strm.PutCString ("No value"); 255cac9c5f9SGreg Clayton } 256cac9c5f9SGreg Clayton 257cac9c5f9SGreg Clayton return true; 258cac9c5f9SGreg Clayton } 259cac9c5f9SGreg Clayton 260