<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Monaco;
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.apple-style-span
{mso-style-name:apple-style-span;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>FWIW, I'd be interested in working on the Windows implementation. I've been knee-deep in *nixes lately and wouldn't mind the refresher. </span><span style='font-size:11.0pt;font-family:Wingdings;color:#1F497D'>J</span><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><b><span style='font-size:9.0pt;font-family:"Verdana","sans-serif";color:navy'>Gordon Keiser<br></span></b><span style='font-size:8.0pt;font-family:"Verdana","sans-serif";color:navy'>Software Development Engineer<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:8.0pt;font-family:"Verdana","sans-serif";color:navy'>Arxan Technologies<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:8.0pt;font-family:"Verdana","sans-serif";color:navy'>w:+1.765.889.4756 <o:p></o:p></span></p><p class=MsoNormal><u><span style='font-size:8.0pt;font-family:"Verdana","sans-serif";color:#0000C0'>gkeiser@arxan.com</span></u><span style='font-size:8.0pt;font-family:"Verdana","sans-serif";color:#E46C0A'> <a href="http://www.arxan.com/"><span style='color:#E46C0A'>www.arxan.com</span></a></span><span style='font-size:8.0pt;font-family:"Verdana","sans-serif";color:#1F497D'> <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><a name="_MailEndCompose"><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></a></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> llvmdev-bounces@cs.uiuc.edu [mailto:llvmdev-bounces@cs.uiuc.edu] <b>On Behalf Of </b>Nick Kledzik<br><b>Sent:</b> Monday, May 07, 2012 3:57 PM<br><b>To:</b> LLVM Developers Mailing List<br><b>Subject:</b> [LLVMdev] [RFC] llvm/include/Support/OutputBuffer.h<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>For the reasons listed in my 03-May-2012 email, I am proposing a new llvm/Support class for using in writing binary files:<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal><span style='font-size:7.5pt;font-family:"Monaco","serif"'>/// OutputBuffer - This interface provides simple way to create an in-memory<br>/// buffer which when done will be written to a file. During the lifetime of <br>/// </span><span class=apple-style-span><span style='font-size:10.0pt;font-family:"Monaco","serif"'>these</span></span> <span style='font-size:7.5pt;font-family:"Monaco","serif"'>objects, the content or existence of the specified file is undefined. <br>/// That is, creating an OutputBuffer for a file may immediately remove the <br>/// file.<br>/// If the OutputBuffer is committed, the target file's content will become <br>/// the buffer content at the time of the commit. If the OutputBuffer is not <br>/// committed, the file will be deleted in the OutputBuffer buffer destructor.<br>class OutputBuffer {<br>public:<br> enum Flags {<br> F_executable = 1, /// set the 'x' bit on the resulting file<br> }; <br><br> /// Factory method to create an OutputBuffer object which manages a read/write<br> /// buffer of the specified size. When committed, the buffer will be written<br> /// to the file at the specified path. <br> static error_code createFile(StringRef filePath, Flags flags, size_t size, <br> OwningPtr<OutputBuffer> &result);<br> <br><br> /// Returns a pointer to the start of the buffer.<br> uint8_t *bufferStart();<br> <br> /// Returns a pointer to the end of the buffer.<br> uint8_t *bufferEnd();<br> <br> /// Returns size of the buffer.<br> size_t size();<br> <br> /// Flushes the content of the buffer to its file and deallocates the <br> /// buffer. If commit() is not called before this object's destructor<br> /// is called, the file is deleted in the destructor. The optional parameter<br> /// is used if it turns out you want the file size to be smaller than<br> /// initially requested.<br> void commit(int64_t newSmallerSize = -1);<br>};</span><o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>The Flags will probable need to be extended over time to handle other clients needs.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>For Unix/Darwin, my plan is to implement this by:<o:p></o:p></p></div><div><p class=MsoNormal>1) delete the file<o:p></o:p></p></div><div><p class=MsoNormal>2) create a new file with a random name in same directory<o:p></o:p></p></div><div><p class=MsoNormal>3) truncate the file to the new size<o:p></o:p></p></div><div><p class=MsoNormal>4) mmap() in the file r/w<o:p></o:p></p></div><div><p class=MsoNormal>5) On commit, unmap the file, rename() to final name<o:p></o:p></p></div><div><p class=MsoNormal>6) In destructor, if not committed, unmap, delete the randomly named file<o:p></o:p></p></div><div><p class=MsoNormal> <o:p></o:p></p></div><div><p class=MsoNormal>I'll leave the windows implementation empty and let someone with windows experience do the implementation.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Comments? Suggestions?<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>-Nick<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p><div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal>On May 3, 2012, at 6:10 PM, Nick Kledzik wrote:<o:p></o:p></p></div></blockquote></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal>Existing llvm code tends to use raw_ostream for writing files. But raw_ostream is not a good match for a linker for a couple of reasons:<br><br>1) When the linker creates an executable, the file needs the 'x' bit set. Currently raw_fd_ostream has no way to set that.<br><br>2) The Unix conformance suite actually has some test cases where the linker is run and the output file does exists but is not writable, or is not writable but is in a writable directory, or with funky umask values. raw_fd_ostream interface has no way to match those semantics.<br><br>3) On darwin we have found the linker performs better if it opens the output file, truncates it to the output size, then mmaps in the file, then writes directly into that memory buffer. This avoids the memory copy from the private buffer to the OS file system buffer in the write() syscall.<br><br>4) In the model we are using for lld, a streaming output interface is not optimal. Currently, lld copies chunks of code from the (read-only) input files, to a temporary buffer, then applies any fixups (relocations), then streams out that temporary buffer. If instead we had a big output buffer, the linker could copy the code chunks directly to the output buffer and apply the fixups there, avoiding an extra copy. <o:p></o:p></p></div><p class=MsoNormal><o:p> </o:p></p></blockquote></div></div></div></body></html>