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