So, I'm trying to use this file to look inside COFF files. Got the header. OK. Now I want to look at the sections. Look, there's a section iterator. I can use that! So, I write: for (llvm::object::section_iterator iter = Obj.begin_sections (); iter != Obj.end_sections(); ++iter ) and it doesn't compile. There's no ++ for section iterators. Apparently, you're supposed to write. for (llvm::object::section_iterator iter = Obj.begin_sections (); iter != Obj.end_sections(); iter.increment(ec)) Srsly? [ And - how do I go from a section_iterator to a coff_section ? ] While I'm puzzling over that, I look some more, and I see: error_code getSection(int32_t index, const coff_section *&Res) const; Cool. (A bit weird; why a signed index?, but whatever) So I write: const llvm::object::coff_section *sect; for (std::size_t i = 0; i < NumSections; ++i) Obj.getSection(i, sect); And my program dies with a segmentation fault. Turns out that sect == NULL. More looking, I see that the sections are numbered 1 … N, not 0 ... N-1 Now I'm really really confused. Why? BTW - patch attached to add pre-increment to llvm::content_iterator (which is the template base for section_iterator, etc) -- Marshall -------------- next part -------------- A non-text attachment was scrubbed... Name: section-iter.patch Type: application/octet-stream Size: 453 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120611/5d4dea4b/attachment.obj> -------------- next part -------------- -- Marshall Marshall Clow Idio Software <mailto:mclow.lists at gmail.com> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki
If you haven't already found it, you should look inside tools/llvm-objdump/llvm-objdump.cpp, which is an easy-to-follow example of how these APIs work --Sean Silva. On Mon, Jun 11, 2012 at 5:41 PM, Marshall Clow <mclow.lists at gmail.com>wrote:> So, I'm trying to use this file to look inside COFF files. > Got the header. OK. > > Now I want to look at the sections. > Look, there's a section iterator. I can use that! > > So, I write: > for (llvm::object::section_iterator iter = Obj.begin_sections (); > iter != Obj.end_sections(); ++iter ) > > and it doesn't compile. There's no ++ for section iterators. > Apparently, you're supposed to write. > for (llvm::object::section_iterator iter = Obj.begin_sections (); > iter != Obj.end_sections(); iter.increment(ec)) > Srsly? > [ And - how do I go from a section_iterator to a coff_section ? ] > > While I'm puzzling over that, I look some more, and I see: > error_code getSection(int32_t index, const coff_section *&Res) > const; > > Cool. (A bit weird; why a signed index?, but whatever) > So I write: > const llvm::object::coff_section *sect; > for (std::size_t i = 0; i < NumSections; ++i) > Obj.getSection(i, sect); > > And my program dies with a segmentation fault. > Turns out that sect == NULL. > More looking, I see that the sections are numbered 1 … N, not 0 ... N-1 > > Now I'm really really confused. Why? > > BTW - patch attached to add pre-increment to llvm::content_iterator (which > is the template base for section_iterator, etc) > > -- Marshall > > > > > > -- Marshall > > Marshall Clow Idio Software <mailto:mclow.lists at gmail.com> > > A.D. 1517: Martin Luther nails his 95 Theses to the church door and is > promptly moderated down to (-1, Flamebait). > -- Yu Suzuki > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120611/267240c4/attachment.html>
On Jun 11, 2012, at 7:39 PM, Sean Silva wrote:> If you haven't already found it, you should look inside tools/llvm-objdump/llvm-objdump.cpp, which is an easy-to-follow example of how these APIs workI had found that - and sadly, it doesn't do what I need to do. In particular, I don't see how to get a coff_section from a section_iterator (and llvm-objdump doesn't do that, either) At the moment, I want to read the section characteristics and the raw data. I guess I can use the index-based calls rather than the iterator-based ones. (One-based? Really?) Seems like a shame, though. Why have two different sets of functionality here? Thanks, though. -- Marshall> > --Sean Silva. > > On Mon, Jun 11, 2012 at 5:41 PM, Marshall Clow <mclow.lists at gmail.com> wrote: > So, I'm trying to use this file to look inside COFF files. > Got the header. OK. > > Now I want to look at the sections. > Look, there's a section iterator. I can use that! > > So, I write: > for (llvm::object::section_iterator iter = Obj.begin_sections (); iter != Obj.end_sections(); ++iter ) > > and it doesn't compile. There's no ++ for section iterators. > Apparently, you're supposed to write. > for (llvm::object::section_iterator iter = Obj.begin_sections (); iter != Obj.end_sections(); iter.increment(ec)) > Srsly? > [ And - how do I go from a section_iterator to a coff_section ? ] > > While I'm puzzling over that, I look some more, and I see: > error_code getSection(int32_t index, const coff_section *&Res) const; > > Cool. (A bit weird; why a signed index?, but whatever) > So I write: > const llvm::object::coff_section *sect; > for (std::size_t i = 0; i < NumSections; ++i) > Obj.getSection(i, sect); > > And my program dies with a segmentation fault. > Turns out that sect == NULL. > More looking, I see that the sections are numbered 1 … N, not 0 ... N-1 > > Now I'm really really confused. Why? > > BTW - patch attached to add pre-increment to llvm::content_iterator (which is the template base for section_iterator, etc) >-- Marshall Marshall Clow Idio Software <mailto:mclow.lists at gmail.com> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120612/16ab42b8/attachment.html>
On Mon, Jun 11, 2012 at 5:41 PM, Marshall Clow <mclow.lists at gmail.com> wrote:> So, I'm trying to use this file to look inside COFF files. > Got the header. OK. > > Now I want to look at the sections. > Look, there's a section iterator. I can use that! > > So, I write: > for (llvm::object::section_iterator iter = Obj.begin_sections (); iter != Obj.end_sections(); ++iter ) > > and it doesn't compile. There's no ++ for section iterators. > Apparently, you're supposed to write. > for (llvm::object::section_iterator iter = Obj.begin_sections (); iter != Obj.end_sections(); iter.increment(ec)) > Srsly? > [ And - how do I go from a section_iterator to a coff_section ? ]That part of the interface is private to COFFObject. COFFObject itself isn't really designed to be used with the generic interface, although I'm very open to a better design that allows this in a sane way.> While I'm puzzling over that, I look some more, and I see: > error_code getSection(int32_t index, const coff_section *&Res) const; > > Cool. (A bit weird; why a signed index?, but whatever) > So I write: > const llvm::object::coff_section *sect; > for (std::size_t i = 0; i < NumSections; ++i) > Obj.getSection(i, sect); > > And my program dies with a segmentation fault. > Turns out that sect == NULL. > More looking, I see that the sections are numbered 1 … N, not 0 ... N-1 > > Now I'm really really confused. Why?This is because that is how they are numbered in COFF. -2, -1, and 0 are special section indices.> BTW - patch attached to add pre-increment to llvm::content_iterator (which is the template base for section_iterator, etc) > > -- MarshallIndex: include/llvm/Object/ObjectFile.h ==================================================================--- include/llvm/Object/ObjectFile.h (revision 158307) +++ include/llvm/Object/ObjectFile.h (working copy) + content_iterator& operator++() { + error_code ec; + return this->increment (ec); + } This is wrong. It ignores the error. operator ++ should be implemented the same way as ++ is in include/llvm/Support/YAMLParser.h. If iteration fails, turn the iterator into an end iterator and set a failed flag and allow the user to access the error_code. - Michael Spencer
Michael, would it be possible for you to plant a strategically placed block comment explaining the situation with the "pointless" error_code stuff, like you explained to me? I'm not sure where is best, but it is worth having written down somewhere. --Sean Silva On Tue, Jun 12, 2012 at 1:08 PM, Michael Spencer <bigcheesegs at gmail.com>wrote:> On Mon, Jun 11, 2012 at 5:41 PM, Marshall Clow <mclow.lists at gmail.com> > wrote: > > So, I'm trying to use this file to look inside COFF files. > > Got the header. OK. > > > > Now I want to look at the sections. > > Look, there's a section iterator. I can use that! > > > > So, I write: > > for (llvm::object::section_iterator iter = Obj.begin_sections (); > iter != Obj.end_sections(); ++iter ) > > > > and it doesn't compile. There's no ++ for section iterators. > > Apparently, you're supposed to write. > > for (llvm::object::section_iterator iter = Obj.begin_sections (); > iter != Obj.end_sections(); iter.increment(ec)) > > Srsly? > > [ And - how do I go from a section_iterator to a coff_section ? ] > > That part of the interface is private to COFFObject. COFFObject itself > isn't really designed to be used with the generic interface, although > I'm very open to a better design that allows this in a sane way. > > > While I'm puzzling over that, I look some more, and I see: > > error_code getSection(int32_t index, const coff_section *&Res) > const; > > > > Cool. (A bit weird; why a signed index?, but whatever) > > So I write: > > const llvm::object::coff_section *sect; > > for (std::size_t i = 0; i < NumSections; ++i) > > Obj.getSection(i, sect); > > > > And my program dies with a segmentation fault. > > Turns out that sect == NULL. > > More looking, I see that the sections are numbered 1 … N, not 0 ... N-1 > > > > Now I'm really really confused. Why? > > This is because that is how they are numbered in COFF. -2, -1, and 0 > are special section indices. > > > BTW - patch attached to add pre-increment to llvm::content_iterator > (which is the template base for section_iterator, etc) > > > > -- Marshall > > > Index: include/llvm/Object/ObjectFile.h > ==================================================================> --- include/llvm/Object/ObjectFile.h (revision 158307) > +++ include/llvm/Object/ObjectFile.h (working copy) > + content_iterator& operator++() { > + error_code ec; > + return this->increment (ec); > + } > > This is wrong. It ignores the error. operator ++ should be implemented > the same way as ++ is in include/llvm/Support/YAMLParser.h. If > iteration fails, turn the iterator into an end iterator and set a > failed flag and allow the user to access the error_code. > > - Michael Spencer > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120612/3b0c7bd6/attachment.html>
Possibly Parallel Threads
- [LLVMdev] Questions about llvm/Object/COFF.h
- [LLVMdev] Questions about llvm/Object/COFF.h
- [LLVMdev] Questions about llvm/Object/COFF.h
- [LLVMdev] Coding Standards: Iterator begin and end functions.
- [LLVMdev] [llvm] r188726 - Adding PIC support for ELF on x86_64 platforms